从本地python脚本执行远程python脚本时出错

时间:2015-06-15 20:23:59

标签: python ssh

我有一个本地python脚本,我试图执行远程python脚本,但我收到错误。这是我local python script中的代码:

cmd_txt = "ssh -v -i /home/user/pem_file.pem user@" + host_name + " 'cd /home/test/ && python python_file.py "+ argument1 +" "+ arguent2 +"'"

system(cmd_txt)

控制台输出和错误是:

OpenSSH_6.2p2, OSSLShim 0.9.8r 8 Dec 2011
debug1: Reading configuration data /etc/ssh_config
debug1: /etc/ssh_config line 20: Applying options for *
debug1: /etc/ssh_config line 102: Applying options for *
debug1: Connecting to _ port 22.
debug1: Connection established.
debug1: identity file /.ssh/id_rsa type 1
debug1: identity file /.ssh/id_rsa-cert type -1
debug1: identity file /id_dsa type -1
debug1: identity file /id_dsa-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.2
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3
debug1: match: OpenSSH_5.3 pat OpenSSH_5*
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Host 'hostname' is known and matches the RSA host key.
debug1: Found key in .ssh/known_hosts:13
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /.ssh/id_rsa
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Trying private key: .ssh/id_dsa
debug1: Next authentication method: password
debug1: read_passphrase: can't open /dev/tty: Device not configured
debug1: permanently_drop_suid: 502
ssh_askpass: exec(/usr/libexec/ssh-askpass): No such file or directory
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
Permission denied, please try again.
debug1: read_passphrase: can't open /dev/tty: Device not configured
debug1: permanently_drop_suid: 502
ssh_askpass: exec(/usr/libexec/ssh-askpass): No such file or directory
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
Permission denied, please try again.
debug1: read_passphrase: can't open /dev/tty: Device not configured
debug1: permanently_drop_suid: 502
ssh_askpass: exec(/usr/libexec/ssh-askpass): No such file or directory
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: No more authentication methods to try.
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).

当我从命令行执行ssh命令时,我没有收到错误,但是在python脚本中它会抛出错误。需要注意的一点是,当我从终端执行它时,我会收到输入密码的通知,但是从python我没有得到任何东西,也不知道如何将密码和ssh一起传递。

此外,远程python脚本又包含对java jar执行的另一个ssh调用。该呼叫的结构类似于上述ssh呼叫。

这里有什么问题以及如何解决这个问题? `

1 个答案:

答案 0 :(得分:1)

您的公钥身份验证无效。这就是为什么当您在终端中尝试命令时,它会要求您输入密码。 日志中的这一行表示它将尝试的身份验证方法的顺序:

  

可以继续的身份验证:publickey,gssapi-keyex,gssapi-with-mic,密码

您会看到密码是最后一个密码,所以如果其他所有密码都失败了,那么它就会重新开始。

当您尝试从Python脚本执行此操作时,它无法启动交互式进程来询问您密码,因此它完全失败。 这就是这条线的含义:

  

debug1:read_passphrase:无法打开/ dev / tty:设备未配置

您需要修复公钥验证,以便在您尝试从Python脚本运行之前从终端ssh时它可以正常工作。