python - ssh-copy-id 有时会挂起

标签 python ssh pexpect

这是我的 question 的“后续”从上周开始。基本上我看到一些使用 pexpect ssh-copy-id 的 python 代码偶尔挂起。

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

2016-07-01 13:23:32 DEBUG           copy command: ssh-copy-id -i /yyy/.ssh/id_rsa.pub <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bbc8d4d6decec8dec9fbc8d4d6de95d2cb" rel="noreferrer noopener nofollow">[email protected]</a>
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', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fc8f939199898f998ebc8f939199d2958c" rel="noreferrer noopener nofollow">[email protected]</a>']
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 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d0a3bfbdb5a5a3b5a290a3bfbdb5feb9a0" rel="noreferrer noopener nofollow">[email protected]</a>

 /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
<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="94e7fbf9f1e1e7f1e6d4e7fbf9f1bafde4" rel="noreferrer noopener nofollow">[email protected]</a>'s password: 
Number of key(s) added: 1
Now try logging into the machine, with:   ....

那么,让我们回顾一下:

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

这让我发疯。欢迎任何想法。

最佳答案

xauth 引用闻起来像是您正在请求 X11 转发。它将在您的 ~/.ssh/config 中进行配置。这可能是您的配置差异导致挂起的原因。

关于python - ssh-copy-id 有时会挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38179980/

相关文章:

python - 是否有一种干净的方法可以仅在第一次迭代时或在执行之前检查循环内的变量?

python 将 1 个键和多个值字典的列表与列表的值合并

linux - 如何在 Linux 中为另一个用户生成 ssh key ,并在 ansible 中自动生成

python - 使用 Paramiko 读取 Top 命令的输出

python - 通过pexpect发送命令时如何避免冗余?

python - 回声$?来自 python

python - 错误: Decorator takes exactly 2 arguments (1 given)

linux - ps -l <​​proc_id> 输出中的 WCHAN 'finish' 是什么意思?

windows - 使用 scp 将本地文件从 Windows 复制到远程服务器

python - 我使用了错误的模块吗? pxssh