从a服务器sftp连接到b服务器,然后使用非交互方式expect的put上传文件到b服务器的指定目录。伪代码如下
expect <<!
spawn sftp -oPort=$PORT $USER@$HOST
expect {
"password:" {
send "$PASS\r"
exp_continue
}
"yes/no" {
send "yes\r"
exp_continue
}
}
expect "sftp"
send_user "---开始上传文件---\n"
send "put 本地文件 远程目录\r"
expect {
"100%" {send_user "---上传文件完成---\n"}
}
send "bye\r"
expect eof
使用脚本执行后发现一直无法上传文件到远端服务器目录。排查过程如下:
1.确认目录权限
2.确认是否设置信任,有服务器端和客户端2种方式,配置方式请自习百度。
image.png
3.确认ssh的配置是否正确。
经过排查后发现是ssh配置导致
更改ssh的GSSAPIAuthentication配置后上传文件成功。GSSAPIAuthentication yes : 基于 GSSAPI 的用户认证,服务器端默认启用了GSSAPI。登陆的时候客户端需要对服务器端的IP地址进行反解析,如果服务器的IP地址没有配置PTR记录,那么就会在这里卡住。
#修改/etc/ssh/sshd_config中的GSSAPIAuthentication no
#使用root修改
GSSAPIAuthentication yes -> GSSAPIAuthentication no
#然后重启ssh
systemctl restart sshd
#查看sshd是否重启成功
systemctl status sshd