简体   繁体   中英

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. 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. 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.

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. 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. 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 . Notice in the exception there is a timeout of 10 seconds when it tries to set_unique_prompt. A workaround for your situation might be to set 'auto_prompt_reset=True' in s.login(). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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