【问题标题】:paramiko, isn't talking to ssh-agent. same behavior in fabricparamiko,没有与 ssh-agent 交谈。织物中的相同行为
【发布时间】:2023-12-26 05:25:02
【问题描述】:

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

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

【问题讨论】:

  • 我也在 Ubuntu 11.10 中使用了 fabric,没有任何问题。听起来两种结构都没有访问与控制台 ssh 相同的密钥文件,或者您使用了错误的登录名。你记得在你的 fabfile 中设置env.user = 'my_user' 吗?
  • 试过了,同样的问题。我猜问题出在 paramiko 而不是面料上。首先让 paramiko 工作将是一个很好的步骤。这就是我创建上面的小测试脚本的原因。
  • 您是否尝试过创建一个新的分支 RSA 密钥,确保它没有密码,将其安装在服务器上,然后改用它?
  • 你试过client.connect('192.168.1.10', password='your_passphrase')吗?我觉得这会有所帮助。
  • 但是 ssh-agent 正在运行,并在我的控制台中进行了身份验证。我可以在不提供密码的情况下通过 ssh 连接到服务器。我的问题是如何用 paramiko 实现这一点。我不想在代码中输入密码,这会否定整个练习的目的。

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


【解决方案1】:

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

您可以通过以下方式为 ssh 库启用更多日志记录:

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

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

【讨论】:

  • 嘿伙计,好提示。我将把额外的信息放入原始帖子中。原来它忽略了指定的用户。
【解决方案2】:

好的,所以我发现的第一件事是 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 进行身份验证的问题。所以这个问题仍然悬而未决。

更新:

感谢 Morgan 的提示,我在这个问题上更进一步。正如他所建议的,我通过在我的 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 错误消息会不会误导我?

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

【讨论】:

  • 你可能在你的 .ssh/config 中有一个配置行,你忘记了或认为这是被尊重的,但我想谨慎起见,把用户留在那里,或者在其中一个实例使用列表推导将它们编码到主机列表中。很高兴这个提示让您找出问题所在。
  • SSH 现在没有维护,他们推荐使用 paramiko。 github.com/bitprophet/ssh
【解决方案3】:

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

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

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

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

【讨论】:

  • 但是 ssh-agent 正在运行,并在我的控制台中进行了身份验证。我可以在不提供密码的情况下通过 ssh 连接到服务器。我的问题是如何用 paramiko 实现这一点。我不想在代码中输入密码,这会否定整个练习的目的。
【解决方案4】:

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

eval $(ssh-agent)

并添加 SSH_KEY:

ssh-add ~/.ssh/id_rsa

【讨论】:

    【解决方案5】:

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

    $ ssh-add ~/.ssh/id_dsa 
    

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

    【讨论】:

    • 我想这就是你的意思:ssh-add ~/.ssh/id_rsa
    最近更新 更多