我用python和paramiko (paramiko==1.15.3版本)ssh通常没有问题。正在做什么:
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('mylinodebox.com', key_filename='key_filename='/home/me/.ssh/id_rsa_linode', port=2222)
stdin, stdout, stderr = ssh.exec_command('ls')
print stdout.readlines()
ssh.close()对我来说,这台linode机器工作得非常好。但对于另一台Azure机器,如果我尝试同样的操作,只需将连接线替换为
ssh.connect('myazurebox.net', key_filename='/home/me/.ssh/the-azure-ssh.key', port=22)我得到了
AuthenticationException: Authentication failed.尽管事实上我在linux终端上使用密钥文件ssh进入Azure机器是完全没有问题的(我做了ssh myazurebox并在下面设置了ssh配置),所以我知道证书是好的。
我的ssh配置文件如下所示
Host *
ForwardAgent yes
ServerAliveInterval 15
ServerAliveCountMax 3
PermitLocalCommand yes
ControlPath ~/.ssh/master-%r@%h:%p
ControlMaster auto
Host myazurebox
HostName myazurebox.net
IdentityFile ~/.ssh/the-azure-ssh.key
User <azureuser>
Host mylinodebox
HostName mylinodebox.com
IdentityFile ~/.ssh/id_rsa_linode
Port 2222有没有人知道为什么这不会起作用?
在导入之后添加行paramiko.common.logging.basicConfig(level=paramiko.common.DEBUG)不会显示更多内容:
DEBUG:paramiko.transport:Ciphers agreed: local=aes128-ctr, remote=aes128-ctr
DEBUG:paramiko.transport:using kex diffie-hellman-group14-sha1; server key type ssh-rsa; cipher: local aes128-ctr, remote aes128-ctr; mac: local hmac-sha1, remote hmac-sha1; compression: local none, remote none
DEBUG:paramiko.transport:Switch to new keys ...
DEBUG:paramiko.transport:Adding ssh-rsa host key for myazurebox.net: 8d596885f13b8e45c1edd7d94bbfa817
DEBUG:paramiko.transport:Trying SSH agent key d403d1c6bec787e486548a3e0fbfa373
DEBUG:paramiko.transport:userauth is OK
INFO:paramiko.transport:Authentication (publickey) failed.
DEBUG:paramiko.transport:Trying SSH agent key 12e9db4c2cd2be32193s78b0b13cb5eb
DEBUG:paramiko.transport:userauth is OK
INFO:paramiko.transport:Authentication (publickey) failed.
DEBUG:paramiko.transport:Trying SSH agent key 1906e3debc819c0f5f40080d43de587d
DEBUG:paramiko.transport:userauth is OK
INFO:paramiko.transport:Authentication (publickey) failed.发布于 2015-10-22 15:50:38
在/var/log/auth.log中设置LogLevel DEBUG之后,我在/etc/ssh/sshd_config上检查了服务器上的日志。结果是,paramiko通过不实际尝试我在ssh.connect参数中提供的密钥文件,尝试使用来自我的本地ssh-agent (可以通过执行``ssh-l列出)中的指纹的密钥。Openssh通过终端也尝试了来自ssh-agent的密钥,最后尝试了密钥文件。
我将azure密钥文件添加到ssh-agent:
ssh-add /home/lee/.ssh/the-azure-ssh.key并再次运行上面的脚本。这一次,paramiko成功建立了连接。
(有人知道为什么将密钥添加到代理中只适用于paramiko,而不适用于密钥文件吗?)
发布于 2015-10-22 17:32:33
我试图重现这个问题,但失败了。
我查看了Python包paramiko的文档,并编写了下面的简单代码。它适用于Azure Linux VM(Ubuntu 1404 LTS)。我的本地操作系统是Ubuntu。
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('<my_vm_name>.cloudapp.net', 22, '<username>', '<password>')
stdin, stdout, stderr = ssh.exec_command('ls')
print stdout.readlines()
ssh.close()https://stackoverflow.com/questions/33271600
复制相似问题