10

首先,我试图让织物工作,但它一直要求我输入密码。

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

5 回答 5

7

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

您可以通过以下方式为 ssh lib 打开它来获取更多日志记录:

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

在你的fabfile中。这将显示所有日志并显示更多 paramiko/ssh 本身正在做什么,这可能有助于您进一步调试问题。

于 2012-03-28T05:01:09.480 回答
5

好的,所以我发现的第一件事是 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-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 错误信息会不会误导我?

我删除了这条线并且它起作用了,所以我猜是这样,这就是答案。

于 2012-03-27T09:49:41.613 回答
1

我会尝试将密码短语指定passwordconnect().

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

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

...这可能是您的情况。

于 2012-03-26T21:26:18.333 回答
0

我遇到了这个问题,启动 SSH_AGENT 的方法对我有用:

eval $(ssh-agent)

并添加 SSH_KEY:

ssh-add ~/.ssh/id_rsa
于 2019-06-04T14:14:13.083 回答
-1

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

$ ssh-add ~/.ssh/id_dsa 

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

于 2012-12-22T14:06:44.750 回答