Bry*_*unt 8 python paramiko ssh-keys fabric 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')
Run Code Online (Sandbox Code Playgroud)
导致以下错误消息:
% ./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
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)
因此,当您执行应该返回列表的a.get_keys()时,从paramiko代码和您的代码.我会看到它的回报.并且它不会返回你可以计算的东西,因为它返回实际的加密密钥位.但无论如何,当你转移到ssh上,并且可行时,让我们转向Fabric.
您可以通过执行以下操作为ssh lib启用更多日志记录:
import ssh
ssh.util.log_to_file("paramiko.log", 10)
Run Code Online (Sandbox Code Playgroud)
在您的fabfile中.这将记录所有日志并显示更多paramiko/ssh本身正在做的事情,这可以帮助您进一步调试问题.
好的,所以我发现的第一件事是Paramiko已过时且无法维护。
现在至少在Ubuntu下它被称为ssh软件包,并且具有不同的维护程序(bitprophet)
这是一个完全按照说明工作的演示类:https : //raw.github.com/bitprophet/ssh/master/demos/demo.py
它需要此文件以用于交互式提示:https : //github.com/bitprophet/ssh/blob/master/demos/interactive.py
这是一个使用它的示例会话:
$ ./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)?-¨-¨¨?
Run Code Online (Sandbox Code Playgroud)
但这并不能回答为什么Fabric未针对正确认为的ssh-agent进行身份验证的问题。因此,问题仍然悬而未决。
更新:
多亏了Morgan的提示,我对这个问题有了进一步的了解。如他的建议,我通过将以下内容添加到fabfile.py的顶部来启用ssh日志记录
from fabric.api import *
import ssh
ssh.util.log_to_file("paramiko.log", 10)
Run Code Online (Sandbox Code Playgroud)
我还监视了服务器日志。这样做时,我发现所指定的用户被忽略了,而是使用了我的本地用户名。
在服务器上:
tail -f /var/log/auth.log
Mar 28 11:12:36 xxxxxxxxxxx sshd[17652]: Invalid user bryan from xxx.xxx.xxx.xxx
Run Code Online (Sandbox Code Playgroud)
本地:
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
Run Code Online (Sandbox Code Playgroud)
嗯,这很奇怪,我将命令运行为: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')
Run Code Online (Sandbox Code Playgroud)
嗯
env.user = 'bryan'
Run Code Online (Sandbox Code Playgroud)
这可能是问题的根源吗?ssh错误消息会误导我吗?
我删除了该行,它起作用了,所以我猜答案是这样。