2015-06-15 151 views
0

我有一个本地python脚本,通过它我试图执行一个远程python脚本,但出现错误。这是我local python script代码:从本地python脚本执行远程python脚本时出错

cmd_txt = "ssh -v -i /home/user/pem_file.pem [email protected]" + 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脚本反过来包含另一个ssh调用java jar执行。该呼叫的结构类似于上述呼叫ssh

什么在这里做错了,以及如何解决这个问题? `

回答

0

您的公钥验证不起作用。这就是为什么当您在终端中尝试命令时,它会询问您的密码。 这条线在日志中显示的验证方法的顺序,它会尝试:

认证,可以继续:公钥,GSSAPI-keyex,GSSAPI与 - 麦克风,密码

你怎么看密码是最后一个,所以如果一切都失败了,它就会回落。

当您尝试从Python脚本执行此操作时,无法启动交互式进程来询问您的密码,以便完全失败。 这就是此行的意思:

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

你需要修复的公钥认证,所以当你的ssh它的工作原理一个终端,然后再尝试使用Python脚本进行工作。

+0

我该如何解决认证问题? –

+0

按照[本指南](https://www.howtoforge.com/set-up-ssh-with-public-key-authentication-debian-etch)之类的内容操作。确保在生成密钥时不要指定密码(只需按Enter键),否则每次ssh时都会提示该密码。 – SillyWilly