Python SSH-paramiko-Azure

Python SSH-paramiko-Azure,python,linux,azure,ssh,paramiko,Python,Linux,Azure,Ssh,Paramiko,使用paramiko(版本paramiko==1.15.3)在python中使用ssh通常没有问题。做: 这台linode机器绝对适合我。但对于另一台Azure机器,如果我尝试同样的方法,只需将连接线替换为 ssh.connect('myazurebox.net', key_filename='/home/me/.ssh/the-azure-ssh.key', port=22) 我明白了 AuthenticationException: Authentication failed. 这是尽管

使用paramiko(版本paramiko==1.15.3)在python中使用ssh通常没有问题。做:

这台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配置),所以我知道creds是好的

我的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

/etc/ssh/sshd\u config
中设置
logleveldebug
之后,我在
/var/log/auth.log
检查了服务器上的日志。事实证明,paramiko是在尝试使用我本地的指纹密钥(可以通过执行`ssh add-l'列出),而不是实际尝试ssh.connect参数中提供的密钥文件。Openssh通过终端还尝试了ssh代理提供的密钥,最后尝试了密钥文件

我将azure密钥文件添加到ssh代理:

ssh-add /home/lee/.ssh/the-azure-ssh.key
然后再次运行上面的脚本。这次paramiko成功建立了连接


(有人知道为什么将密钥添加到代理可以与paramiko一起工作,而不仅仅是密钥文件吗?

我试图重现该问题,但失败了

我查看了Python包的doc
paramiko
,并编写了如下简单代码。它适用于Azure Linux虚拟机(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()
导入paramiko
ssh=paramiko.SSHClient()
ssh.set_缺少_主机_密钥_策略(paramiko.AutoAddPolicy())
ssh.connect('.cloudapp.net',22','','')
stdin,stdout,stderr=ssh.exec_命令('ls'))
打印stdout.readlines()
ssh.close()

您没有使用keys@fpghost谢谢你的回答!我重现了这个问题,您的答案是目前唯一一个使用keyfile的工作解决方案。
ssh-add /home/lee/.ssh/the-azure-ssh.key
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()