Shoaib Mohammed
Shoaib Mohammed

Reputation: 1036

paramiko-expect timing out

Following is my code:

import paramiko
from paramiko_expect import SSHClientInteraction

remote_conn_pre = paramiko.SSHClient()
remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())
remote_conn_pre.connect(ip, username=username, password=password, allow_agent=False, look_for_keys=False)

print ("SSH connection established to %s" % ip)

interact = SSHClientInteraction(remote_conn_pre,timeout=100,display=True)
interact.expect()
interact.send(command)
cmd_output_uname = interact.current_output_clean
print cmd_output_uname

Note: I have defined username, password, prompt in my program

At interact.expect() it sticks, and it never reaches interact.send(command).

After a while it times out:

pktgen@pktgen:~$ 
Traceback (most recent call last):
  File "C:\Python27\paramikores.py", line 29, in <module>
    interact.expect()
  File "build\bdist.win32\egg\paramiko_expect.py", line 119, in expect
    current_buffer = self.channel.recv(self.buffer_size)
  File "C:\Python27\lib\site-packages\paramiko\channel.py", line 615, in recv
    raise socket.timeout()
timeout

But it should have shown the output from ls.

Please help me with this? Am I missing something?

Upvotes: 0

Views: 1508

Answers (2)

RCross
RCross

Reputation: 5118

As already mentioned, you must define the prompt you are expecting, but also note that it's read as a regular expression; therefore, special characters (like '$') must be escaped. I generally set my prompts as follows:

prompt = '.*\$\s+'
sudo_prompt = '\[sudo\] password .*:\s+'
root_prompt = '.*\#\s+'

Upvotes: 0

Kadir
Kadir

Reputation: 1762

You would define a prompt which you were expecting after the ssh connection established and then use it in the expect like:

prompt = '<username>@<remote_server_name>:~\$'
interact.expect(prompt)

Upvotes: 1

Related Questions