【问题标题】:ssh-copy-id hanging sometimesssh-copy-id 有时挂起
【发布时间】:2016-07-04 08:25:11
【问题描述】:

这是对我上周的question 的“跟进”。基本上我看到一些使用 pexpect 的 ssh-copy-id 的 python 代码 偶尔 挂起。

我认为这可能是一个 pexect 问题,但在我能够从这样的挂起中收集“堆栈跟踪”之后,我不再那么确定了。 在这里你可以看到我的应用程序的一些痕迹;接下来是运行到超时后的堆栈跟踪:

2016-07-01 13:23:32 DEBUG           copy command: ssh-copy-id -i /yyy/.ssh/id_rsa.pub someuser@some.ip
2016-07-01 13:23:33 DEBUG           first expect: 1
2016-07-01 13:23:33 DEBUG           sending PASSW0RD
2016-07-01 13:23:33 DEBUG           consuming output from remote side ... 
2016-07-01 13:24:03 INFO            Timeout occured ... stack trace information ...

2016-07-01 13:24:03 INFO            Traceback (most recent call last):


File "/usr/local/lib/python3.5/site-packages/pexpect-3.3-py3.5.egg/pexpect/__init__.py", line 1535, in expect_loop
c = self.read_nonblocking(self.maxread, timeout)
File "/usr/local/lib/python3.5/site-packages/pexpect-3.3-py3.5.egg/pexpect/__init__.py", line 968, in read_nonblocking
raise TIMEOUT('Timeout exceeded.')
pexpect.TIMEOUT: Timeout exceeded.

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
 File "xxx/PrepareSsh.py", line 28, in execute
   self.copy_keys(context, user, timeout)
 File "xxx/PrepareSsh.py", line 83, in copy_keys
   child.expect('[#\$]')
 File "/usr/local/lib/python3.5/site-packages/pexpect-3.3-py3.5.egg/pexpect/__init__.py", line 1451, in expect
timeout, searchwindowsize)
 File "/usr/local/lib/python3.5/site-packages/pexpect-3.3-py3.5.egg/pexpect/__init__.py", line 1466, in expect_list
timeout, searchwindowsize)
 File "/usr/local/lib/python3.5/site-packages/pexpect-3.3-py3.5.egg/pexpect/__init__.py", line 1568, in expect_loop
raise TIMEOUT(str(err) + '\n' + str(self))
pexpect.TIMEOUT: Timeout exceeded.
<pexpect.spawn object at 0x2b74694995c0>
version: 3.3
command: /usr/bin/ssh-copy-id
args: ['/usr/bin/ssh-copy-id', '-i', '/yyy/.ssh/id_rsa.pub', 'someuser@some.ip']
searcher: <pexpect.searcher_re object at 0x2b746ae1c748>
buffer (last 100 chars): b'\r\n/usr/bin/xauth:  creating new authorityy file /home/hmcmanager/.Xauthority\r\n'
before (last 100 chars): b'\r\n/usr/bin/xauth:  creating new authority file /home/hmcmanager/.Xauthority\r\n'
after: <class 'pexpect.TIMEOUT'>

所以,我觉得有点奇怪:xauth 出现在 pexpect 收到的消息中。

你看,今天,我创建了另一个虚拟机进行测试;并手动完成所有设置。这是我这样做时看到的:

> ssh-copy-id -i ~/.ssh/id_rsa.pub someuser@some.ip

 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/xxx/.ssh/id_rsa.pub"
The authenticity of host 'some.ip (some.ip)' can't be established.
ECDSA key fingerprint is SHA256:7...
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
someuser@some.ip's password: 
Number of key(s) added: 1
Now try logging into the machine, with:   ....

所以,让我们回顾一下:

  • 当我手动运行 ssh-copy-id ... 一切正常;并且字符串“xauth”没有出现在返回的输出中
  • 当我以编程方式运行 ssh-copy-id 时,它大部分时间都有效;但有时会出现超时......关于 xauth 的消息会发送给我的客户

这快把我逼疯了。欢迎任何想法。

【问题讨论】:

  • 您期待以下命令提示符:child.expect('[#\$]')。但是从您的手动输出来看,您的提示看起来像 &gt;
  • 我正在尝试使用我可以使用的正则表达式。你能提供一个简单地匹配“尽可能”的吗?真正“消耗”一切直到EOF?而且我不确定您在说什么:“>”这是我的客户端系统上的提示。这与 pexect 用于对从远程系统返回的输入做出反应的模式有何关系?
  • 试试这个 child.expect('[#>:]')
  • xauth 闻起来像是在请求 X11 转发。你有它在你的~/.ssh/config 或其他地方吗?
  • @Jakuje 哦,是的;里面有ForwardX11=yesForwardX11Trusted=yes。和 uups,这是我的本地工作站和我的“登录”服务器之间的区别......我的自动化脚本在其上运行......并挂起。

标签: python ssh pexpect


【解决方案1】:

xauth 参考闻起来就像您在请求 X11 转发。它将在您的~/.ssh/config 中配置。这可能是导致挂起的配置差异。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-11
    • 2020-12-20
    • 1970-01-01
    • 2018-06-30
    • 2014-04-27
    • 2014-10-28
    • 2014-05-07
    • 2015-11-15
    相关资源
    最近更新 更多