paramiko,不是和ssh-agent谈话。在织物中的相同行为

时间:2012-03-23 17:29:17

标签: python fabric paramiko ssh-keys ssh-agent

首先,我试图让织物工作,但它一直在问我密码。

所以我试图减少这个问题。也许从Python创建SSH连接是一个很好的POC。我发现Fabric使用parmiko进行SSH处理。嗯。好的,让我们试着让一个例子工作。

这是我写的。

from ssh import *
import os 

print "SSH-AGENT VARS"

print "SSH_AGENT_PID: %s " % os.environ['SSH_AGENT_PID']
print "SSH_AUTH_SOCK: %s " % os.environ['SSH_AUTH_SOCK']

a = Agent()
keys=a.get_keys()
print keys.count("192.168.1.10")


client = SSHClient()
client.load_system_host_keys()
client.connect('192.168.1.10')

导致以下错误消息:

% ./ssh_test.py
SSH-AGENT VARS
SSH_AGENT_PID: 26557 
SSH_AUTH_SOCK: /tmp/ssh-pZHBElj26556/agent.26556 
0
Traceback (most recent call last):
  File "./ssh_test.py", line 18, in <module>
    client.connect('192.168.1.10')
  File "/usr/local/lib/python2.7/dist-packages/ssh/client.py", line 332, in connect
    self._auth(username, password, pkey, key_filenames, allow_agent, look_for_keys)
  File "/usr/local/lib/python2.7/dist-packages/ssh/client.py", line 493, in _auth
    raise saved_exception
ssh.PasswordRequiredException: Private key file is encrypted

ssh-agent正在我的会话中运行,我可以通过SSH连接到该框,没有问题,它不会提示我输入密码或任何东西。

我猜猜paramiko因某些奇怪的原因无法连接到正在运行的ssh-agent。

还有其他人有这样的问题吗?我正在使用Ubuntu 11.10

我似乎记得曾经尝试过Fabric并遇到类似的问题,也许它已经被打破了一段时间?

我连接,只使用主机名作为参数。这是根据文档。

http://www.lag.net/paramiko/docs/paramiko.SSHClient-class.html

connect(self, hostname, port=22, username=None, password=None, pkey=None, key_filename=None, timeout=None, allow_agent=True, look_for_keys=True, compress=False)

5 个答案:

答案 0 :(得分:6)

所以当你执行应该返回列表的a.get_keys()时,从paramiko代码和你的代码。我会看到它的回报。并且它不会返回你可以计算的东西,因为它返回实际的加密密钥位。但无论如何,当你转移到ssh上,并且可行时,让我们转向Fabric。

您可以通过执行以下操作为ssh lib启用更多日志记录:

import ssh
ssh.util.log_to_file("paramiko.log", 10)

在你的fabfile中。这将记录所有日志并显示更多paramiko / ssh本身正在做的事情,这可以帮助您进一步调试问题。

答案 1 :(得分:4)

好的,所以我发现的第一件事就是Paramiko已经过时了,而且没有维护。

它现在被称为包ssh,至少在Ubuntu下,并且有一个不同的维护者(bitprophet)

以下是使用它的示例会话:

$ ./ssh_demo.py
Hostname: 192.168.1.10
*** Host key OK.
Username [bryan]: root
Trying ssh-agent key eee5638f390e1698898984b10adfa9317 ... success!
*** Here we go!

Linux top.secret.com 2.9.37-1-amd64 #1 SMP Thu Nov 3 03:41:26 UTC 2011 x86_64
┌┌(root@top)-(10:44am-:-03/27)┌-¨-¨¨˙

这并没有回答为什么结构没有正确认为ssh-agent进行身份验证的问题。所以问题仍然存在。

更新

感谢摩根的暗示,我对这个问题有了进一步的了解。正如他所建议的,我通过在fabfile.py

的顶部添加以下内容来启用ssh日志记录
from fabric.api import *
import ssh
ssh.util.log_to_file("paramiko.log", 10)

我还监控了服务器日志。在这样做的过程中,我发现我指定的用户被忽略了,而是使用了我的本地用户名。

在服务器上:

tail -f /var/log/auth.log 
Mar 28 11:12:36 xxxxxxxxxxx sshd[17652]: Invalid user bryan from xxx.xxx.xxx.xxx

本地:

tail -f paramiko.log    
DEB [20120328-11:39:29.038] thr=1   ssh.transport: starting thread (client mode): 0x8dfc66cL
INF [20120328-11:39:29.066] thr=1   ssh.transport: Connected (version 2.0, client OpenSSH_5.5p1)
DEB [20120328-11:39:29.093] thr=1   ssh.transport: kex algos:['diffie-hellman-group-exchange-sha256', 'diffie-hellman-group-exchange-sha1', 'diffie-hellman-group14-sha1', 'diffie-hellman-group1-sha1'] server key:['ssh-rsa', 'ssh-dss'] client encrypt:['aes128-ctr', 'aes192-ctr', 'aes256-ctr', 'arcfour256', 'arcfour128', 'aes128-cbc', '3des-cbc', 'blowfish-cbc', 'cast128-cbc', 'aes192-cbc', 'aes256-cbc', 'arcfour', 'rijndael-cbc@lysator.liu.se'] server encrypt:['aes128-ctr', 'aes192-ctr', 'aes256-ctr', 'arcfour256', 'arcfour128', 'aes128-cbc', '3des-cbc', 'blowfish-cbc', 'cast128-cbc', 'aes192-cbc', 'aes256-cbc', 'arcfour', 'rijndael-cbc@lysator.liu.se'] client mac:['hmac-md5', 'hmac-sha1', 'umac-64@openssh.com', 'hmac-ripemd160', 'hmac-ripemd160@openssh.com', 'hmac-sha1-96', 'hmac-md5-96'] server mac:['hmac-md5', 'hmac-sha1', 'umac-64@openssh.com', 'hmac-ripemd160', 'hmac-ripemd160@openssh.com', 'hmac-sha1-96', 'hmac-md5-96'] client compress:['none', 'zlib@openssh.com'] server compress:['none', 'zlib@openssh.com'] client lang:[''] server lang:[''] kex follows?False
DEB [20120328-11:39:29.093] thr=1   ssh.transport: Ciphers agreed: local=aes128-ctr, remote=aes128-ctr
DEB [20120328-11:39:29.093] thr=1   ssh.transport: using kex diffie-hellman-group1-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
DEB [20120328-11:39:29.183] thr=1   ssh.transport: Switch to new keys ...
DEB [20120328-11:39:29.224] thr=2   ssh.transport: Trying SSH agent key cda5638f390e166864444b1093b91017
DEB [20120328-11:39:29.272] thr=1   ssh.transport: userauth is OK
INF [20120328-11:39:53.310] thr=1   ssh.transport: Authentication (publickey) failed.
DEB [20120328-11:41:29.076] thr=1   ssh.transport: EOF in transport thread
嗯,这很奇怪,我把命令运行为:     fab diskfree -H xxx.xxx.xxx.xxx -u root

但这是什么?

$ cat ./fabfile.py
from fabric.api import *
import ssh
ssh.util.log_to_file("paramiko.log", 10)

env.user = 'bryan'

def host_type():
 run('uname -s')

def diskfree():
 run('df -h') 

env.user = 'bryan'

这可能是问题的根源吗? ssh错误消息会误导我吗?

我删除了该行并且它有效,所以我想是的,就是答案。

答案 2 :(得分:0)

我会尝试将密码指定为password的关键字connect()参数。

正如SSHCLient.connect()的文档中所述,它使用了系统中可以找到的任何PKey,如果没有提供特定的from_private_key()。类方法from_private_key_file()和{{3}}(我不确定它们中的哪一个被调用,可能两者都是)采用可选参数password。文档说,

  

如果私钥被加密且密码不是None,则使用给定的密码来解密密钥(否则抛出PasswordRequiredException)。

......这可能是你案件中发生的事情。

答案 3 :(得分:0)

我遇到了这个问题,而对我来说起作用的是启动SSH_AGENT的原因:

eval $(ssh-agent)

并添加SSH_KEY:

ssh-add ~/.ssh/id_rsa

答案 4 :(得分:-1)

也许您需要运行,将密钥添加到代理。

$ ssh-add ~/.ssh/id_dsa 

https://groups.google.com/forum/?fromgroups=#!topic/ansible-project/yRSMmlqKsAA