paramiko-expect icon indicating copy to clipboard operation
paramiko-expect copied to clipboard

Paramiko-expect Timeout Issue

Open anuragsatish opened this issue 6 years ago • 15 comments

Hi Team,

I'm using paramiko expect for automating one of my terminal session, however I'm facing an issue with the prompt, please find below the error

>>> import paramiko
>>> import re
>>> from paramiko_expect import SSHClientInteraction
>>> 
>>> client = paramiko.SSHClient()
>>> client.load_system_host_keys()
>>> client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> client.connect(hostname='172.19.0.120', username='default', password='default', port =2222)
>>> interact = SSHClientInteraction(client, timeout=5, encoding='utf-8', display=True, buffer_size=100000)
>>> prompt = re.escape('.*$  ')
>>> interact.expect(prompt)
CFS:default@Z18044492 [~]$ 
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/paramiko/channel.py", line 683, in recv
    out = self.in_buffer.read(nbytes, self.timeout)
  File "/usr/local/lib/python3.5/dist-packages/paramiko/buffered_pipe.py", line 160, in read
    raise PipeTimeout()
paramiko.buffered_pipe.PipeTimeout

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/dist-packages/paramiko_expect.py", line 144, in expect
    current_buffer = self.channel.recv(self.buffer_size)
  File "/usr/local/lib/python3.5/dist-packages/paramiko/channel.py", line 685, in recv
    raise socket.timeout()
socket.timeout
>>>

Please help me on how to solve this issue?

anuragsatish avatar Jul 09 '18 07:07 anuragsatish

Why are you escaping the prompt with re.escape? The library needs a regex to match against and by escaping it, you've essentially removed its purpose there 😄

fgimian avatar Jul 09 '18 08:07 fgimian

hi fgimian,

I've tried the expect without escaping by directly using regex, however i'm still facing the same issue,

>>> import paramiko
>>> import re
>>> from paramiko_expect import SSHClientInteraction
>>> client = paramiko.SSHClient()
>>> client.load_system_host_keys()
>>> client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> client.connect(hostname='172.19.0.120', username='default', password='default', port =2222)
>>> interact = SSHClientInteraction(client, timeout=5, encoding='utf-8', display=True, buffer_size=100000)
>>> interact.expect('.*\$\s+')
CFS:default@Z18044492 [~]$ Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/paramiko/channel.py", line 683, in recv
    out = self.in_buffer.read(nbytes, self.timeout)
  File "/usr/local/lib/python3.5/dist-packages/paramiko/buffered_pipe.py", line 160, in read
    raise PipeTimeout()
paramiko.buffered_pipe.PipeTimeout

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/dist-packages/paramiko_expect.py", line 144, in expect
    current_buffer = self.channel.recv(self.buffer_size)
  File "/usr/local/lib/python3.5/dist-packages/paramiko/channel.py", line 685, in recv
    raise socket.timeout()
socket.timeout
>>> 

-Anurag

anuragsatish avatar Jul 09 '18 09:07 anuragsatish

$ sysmbol is escaped in the expect(prompt)

anuragsatish avatar Jul 09 '18 09:07 anuragsatish

Any Update on this issue? I'm experiencing a similar issue

manmeetsaini avatar Oct 05 '18 20:10 manmeetsaini

It is not something I can reproduce here and although I wrote this library, I don't use it actively in my projects anymore. I welcome contributions from the community, there's not a lot of code here so it should be pretty easy to figure out if you have an environment where the problem is reproducible.

Kindest Regards Fotis

fgimian avatar Oct 06 '18 10:10 fgimian

I'm also facing the same issue .Any update on the issue

sreethal avatar Oct 31 '18 06:10 sreethal

I think I found the problem. As a quick fix, you can change default_match_prefix to '.*' in first expect call:

interact.expect(PROMPT, default_match_prefix='.*')

ymkim92 avatar Nov 01 '18 00:11 ymkim92

It's not working for me.

Code:


import paramiko from paramiko_expect import SSHClientInteraction import sys import re

host="x.x.x.x" user="" pw="*" prompt='#'

ssh=paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(host, username=user, password=pw) interact = SSHClientInteraction(ssh, timeout=20, display=True) interact.expect(prompt,timeout=10,default_match_prefix='.*') ssh.close()

Prompt:


jagctrlreg(15)#

Error:


Traceback (most recent call last): File "/opt/sreethalenv/lib/python3.6/site-packages/paramiko/channel.py", line 683, in recv out = self.in_buffer.read(nbytes, self.timeout) File "/opt/sreethalenv/lib/python3.6/site-packages/paramiko/buffered_pipe.py", line 160, in read raise PipeTimeout() paramiko.buffered_pipe.PipeTimeout

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "sshprompt.py", line 16, in interact.expect(prompt,timeout=10,default_match_prefix='.*') File "/opt/sreethalenv/lib/python3.6/site-packages/paramiko_expect.py", line 144, in expect current_buffer = self.channel.recv(self.buffer_size) File "/opt/sreethalenv/lib/python3.6/site-packages/paramiko/channel.py", line 685, in recv raise socket.timeout() socket.timeout

sreethal avatar Nov 02 '18 06:11 sreethal

If anyone can provide me readonly SSH access to a server that causes this issue, I'd be appy to take a look.

If not, perhaps paste your $PS1 variable as it may be all I need to reproduce this.

fgimian avatar Nov 04 '18 09:11 fgimian

In my use case, I am supposed to enter about 2 to 4 password consecutively (I mean password 3 and 4 may or may not be prompted for) .I got the same 'socket.timeout' exception when I tried, with a timeout value. I used it to my advantage by using a try..except..else block

        try:
            interact.expect(prompt,timeout=2)
        except:
            pass
        else:
            interact.send(pass1)

Just in case if this helps.

Seethaar avatar Dec 27 '18 05:12 Seethaar

This worked for me
interact.expect('.*<pattern>.*')

sahilmodak1 avatar Jan 24 '19 00:01 sahilmodak1

I had the same issue, just changed the prompt on the very first command I send to the remote machine prompt="AUTO>" interact.send(f"PS1='{prompt}'") and keep running

cojayero avatar Jun 14 '19 13:06 cojayero

@anuragsatish is any of the suggestions worked for you ? I.e. changing the PS1 ?

fruch avatar Jul 24 '19 19:07 fruch

output1 = device.send_config_set(commands, exit_config_mode=False)

This extended the number of times a loop run in my script. But didn't get rid completely of the PipeTimeout issue.

nontha avatar Apr 15 '20 17:04 nontha

@nontha In some cases we may need to increase the timeout and also set keepalive to avoid socket to close the connection. This happens when you open the connection and keep it idle for sometime. PFB

                self._connection = SSHClientInteraction(self.client, timeout=60, display=True, buffer_size=2048)
                self._connection.channel.transport.set_keepalive(2)

vinaykumar-c avatar Aug 05 '20 03:08 vinaykumar-c