【问题标题】:SSH paramiko AzureSSH paramiko Azure
【发布时间】:2015-10-22 00:40:52
【问题描述】:

我通常在 python 中使用 paramiko(版本 paramiko==1.15.3)进行 ssh'ing 没有问题。正在做:

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'ing 到 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.

【问题讨论】:

    标签: python linux azure ssh paramiko


    【解决方案1】:

    /etc/ssh/sshd_config 中设置LogLevel DEBUG 后,我检查了/var/log/auth.log 服务器上的日志。事实证明,paramiko 正在尝试使用本地 ssh-agent(可以通过执行 `ssh-add -l 列出)的指纹密钥,而实际上并未尝试我在 ssh.connect 的参数中提供的密钥文件。 Openssh 通过终端也尝试了来自 ssh-agent 的密钥,然后终于尝试了密钥文件。

    我将 azure 密钥文件添加到 ssh-agent:

    ssh-add /home/lee/.ssh/the-azure-ssh.key
    

    然后再次运行上面的脚本。这次paramiko成功建立了连接。

    (有谁知道为什么将密钥添加到代理可以使用 paramiko 而不是单独使用密钥文件?)

    【讨论】:

      【解决方案2】:

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

      我查看了 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()
      

      【讨论】:

      • 你没有使用密钥
      • @fpghost 感谢您的回答!我重现了这个问题,你的回答是目前使用密钥文件的唯一一个工作解决方案。