paramiko-期待超时

时间:2016-11-11 11:13:05

标签: python-2.7 paramiko

以下是我的代码:

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

注意:我已在程序中定义了usernamepassword,提示

interact.expect()它会坚持,而且永远不会达到interact.send(command)

过了一会儿就超时了:

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

但它应该显示ls的输出。

请帮帮我吗?我错过了什么吗?

2 个答案:

答案 0 :(得分:1)

您将在建立ssh连接后定义一个您期望的提示,然后在期望中使用它:

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

答案 1 :(得分:0)

如前所述,您必须定义您期望的提示,但请注意它被读作正则表达式;因此,必须转义特殊字符(如'$')。我通常按​​如下方式设置提示:

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