我正在尝试使用 pexpect 模块对文件进行 SFTP。
sftp_opts = ['-o', 'Port=%s' % port,
'-o', 'UserKnownHostsFile=%s' % known_hosts_file,
'-o', 'PasswordAuthentication=yes',
'%s@%s' % (user, host)]
p = pexpect.spawn('sftp', sftp_opts)
try:
p.expect('(?i)password:')
x = p.sendline(password)
x = p.expect('sftp>')
x = p.sendline('cd ' + remote_dir)
x = p.expect('sftp>')
x = p.sendline('put ' + filename)
x = p.expect('sftp>')
x = p.isalive()
x = p.close()
retval = p.exitstatus
except pexpect.EOF:
print('SFTP file transfer failed due to premature end of file.')
return False
except pexpect.TIMEOUT:
print('SFTP file transfer failed due to timeout.')
return False
看起来我能够通过 SSH 连接并获得身份验证,但 retval 始终为 1(退出状态)并且文件没有被 sftp 处理。
我在这里错过了什么吗?
如果我尝试等待 p (p.wait() 而不是 p.close()) - 它永远不会返回。