简体   繁体   English

服务器无法访问IP时,使用pxssh的登录超时时间太长

[英]Login timeout with pxssh takes too long when the server is IP unreachable

I am trying to parse data from series of servers via SSH and would like to shorten time for the SSH attemption when the server doesn't have IP connectivity. 我正在尝试通过SSH解析来自一系列服务器的数据,并希望在服务器没有IP连接时缩短SSH尝试的时间。 It seems it takes about 20 seconds until the program raises exception and it makes the total processing time too long to attempt all servers. 该程序引发异常似乎需要大约20秒钟,并且使总处理时间太长,无法尝试所有服务器。 I tried changing timeout parameters like below but it didn't change anything. 我尝试更改超时参数,如下所示,但它没有任何改变。

import pexpect
from pexpect import pxssh
s = pxssh.pxssh(timeout=1)
s.login(ipaddr, username, password, login_timeout=1)

Please advise me how I can shorten the waiting time to like 5 seconds or shorter. 请告诉我如何将等待时间缩短到5秒或更短。

Thanks 谢谢

I think that the delay you're experiencing does not have to do with the time to connect, but rather it is due to the time that pxssh spends doing other things once it does connect. 我认为您遇到的延迟与连接时间无关,而是由于pxssh在连接后花时间做其他事情。 Here is what I've tried. 这是我尝试过的。

This raises an exception almost immediately: 这几乎立即引发异常:

>>> s = pxssh.pxssh()
>>> s.login('google1.com', 'user')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File     "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pexpect/pxssh.py", line 297, in login
raise ExceptionPxssh('Could not establish connection to host')
pexpect.pxssh.ExceptionPxssh: Could not establish connection to host

But, this takes about 10 seconds before an exception is raised, and changing 'login_timeout' seems to have no effect. 但是,这大约需要10秒钟才能引发异常,并且更改'login_timeout'似乎无效。 The exception is very long so I'll just post part of it below: 例外很长,因此我将其一部分发布在下面:

>>> from pexpect import pxssh
>>> s = pxssh.pxssh()
>>> s.login('google.com', 'user')
...
 File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pexpect/pxssh.py", line 401, in set_unique_prompt
i = self.expect ([TIMEOUT, self.PROMPT], timeout=10)

I think the problem has to do with the way that pxssh resolves the terminal prompt, you can read more about the process here . 我认为问题与pxssh解决终端提示的方式有关,您可以在此处阅读有关该过程的更多信息。 Notice in the exception there is a timeout of 10 seconds when it tries to set_unique_prompt. 请注意,在尝试设置set_unique_prompt时,有一个10秒钟的超时例外。 A workaround for your situation might be to set 'auto_prompt_reset=True' in s.login(). 解决您的情况的方法可能是在s.login()中设置'auto_prompt_reset = True'。 This removed the delay for me. 这为我消除了延迟。

In my case, the servers that took a long time to login were ones where I had a custom prompt setup. 就我而言,花费很长时间登录的服务器是我进行了自定义提示设置的服务器。 I backed up and removed my .bashrc and the login was much faster. 我备份并删除了.bashrc,登录速度更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM