Python 帕拉米科,isn';不要和ssh代理说话。织物中的相同行为

Python 帕拉米科,isn';不要和ssh代理说话。织物中的相同行为,python,fabric,paramiko,ssh-keys,ssh-agent,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 " %

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

所以我试图减少这个问题。也许仅仅从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

我将尝试将密码短语指定为
password
参数,以
connect()

如文档中所述,如果没有提供特定的,它将使用系统中可以找到的任何内容。类和方法(我不确定调用了哪一个,可能两者都调用)采用可选参数
password
。医生说

如果私钥已加密且密码不是None,则将使用给定的密码解密密钥(否则将引发PasswordRequiredException)


…这可能就是你的情况。

好的,所以我发现的第一件事是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)┌-¨-¨¨˙
这并不能回答为什么fabric不能正确地针对ssh代理进行身份验证的问题。因此,问题仍然悬而未决

更新:

多亏了摩根的暗示,我对这个问题有了进一步的了解。正如他所建议的,我通过在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') 

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


我删除了这行代码,它工作了,所以我想答案是这样的。

因此,从paramiko代码和您的代码中,当您执行a.get_keys()时,应该会返回一个列表。我要看看它会有什么回报。它不会返回你可以计算的东西,因为它返回的是实际的加密密钥位。但不管怎样,既然您已经转到ssh,而且这很有效,那么让我们转到Fabric

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

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

在你的档案里。这将显示所有日志,并显示paramiko/ssh本身正在做的更多事情,这可能有助于您进一步调试问题。

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

$ ssh-add ~/.ssh/id_dsa 

我遇到了这个问题,启动SSH\u代理对我有什么好处:

eval $(ssh-agent)
并添加SSH_密钥:

ssh-add ~/.ssh/id_rsa

我也在Ubuntu 11.10中使用fabric,没有任何问题。听起来可能是fabric没有访问与控制台ssh相同的密钥文件,或者您使用了错误的登录。你记得在你的文件中设置
env.user='my_user'
吗?试过了,同样的问题。我猜问题在于paramiko,而不是织物。首先让帕拉米科工作将是一个很好的步骤。这就是我创建上面的小测试脚本的原因。您是否尝试过创建一个新的RSA密钥分支,确保它没有密码短语,在服务器上安装并使用它?您是否尝试过
client.connect('192.168.1.10',password='your_passphrase')
?我觉得这可能会有所帮助。但是ssh代理正在运行,并且在我的控制台中进行了身份验证。我可以在不提供密码的情况下通过ssh连接到服务器。我的问题是如何与帕拉米科实现这一点。我不想在代码中输入密码,这将否定整个练习的目的。但是ssh代理正在运行,并且在我的控制台中进行了身份验证。我可以在不提供密码的情况下通过ssh连接到服务器。我的问题是如何与帕拉米科实现这一点。我不想把密码放在代码里,那会否定整个练习的目的。嘿,伙计,很好的提示。我将把额外的信息放在原来的帖子里。结果是忽略了指定的用户。您可能在.ssh/config中有一个配置行,您忘记了,或者认为这是值得尊敬的,但我喜欢谨慎行事,将用户保留在那里,或者在其中一个实例中,使用列表理解将其编码到主机列表中。很高兴这个提示让您了解了这个问题。SSH现在没有维护,他们建议使用paramiko。我想这就是你的意思:ssh add~/.ssh/id\u rsa
eval $(ssh-agent)
ssh-add ~/.ssh/id_rsa