fgimian / paramiko-expect

A Python expect-like extension for the Paramiko SSH library which also supports tailing logs.
MIT License
205 stars 78 forks source link

Paramiko-expect Timeout Issue #43

Open anuragsatish opened 6 years ago

anuragsatish commented 6 years ago

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?

fgimian commented 6 years ago

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 😄

anuragsatish commented 6 years ago

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 commented 6 years ago

$ sysmbol is escaped in the expect(prompt)

manmeetsaini commented 6 years ago

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

fgimian commented 6 years ago

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

sreethal commented 6 years ago

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

ymkim92 commented 6 years ago

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='.*')
sreethal commented 6 years ago

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

fgimian commented 6 years ago

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.

Seethaar commented 5 years ago

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.

sahilmodak1 commented 5 years ago
This worked for me
interact.expect('.*<pattern>.*')
cojayero commented 5 years ago

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

fruch commented 5 years ago

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

nontha commented 4 years ago

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.

vinaykumar-c commented 4 years ago

@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)