【发布时间】:2014-12-28 19:29:24
【问题描述】:
我需要一些有关 paramiko(python) 的帮助。我有一个带有非标准 SSH 接口的服务器,所以我使用了交互模式,并且我在同一个通道下运行了几个命令(exec_command 对我不起作用)。一切都很好,但是我想为每个命令引入一些超时,因为当没有收到数据时应用程序会停留在 while 循环中。 Channel.settimeout 似乎无法正常工作,因为应用程序在第一个命令后超时。由于线程,我不能使用信号。当我使用 time() 计算数据变量为空后的时间时,我注意到整个代码执行挂起,很可能正在等待通道数据。任何建议如何解决这个问题将不胜感激。
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
connect = ssh.connect(host,username=,password=,timeout=20)
file=open("test.txt","w+")
channel = ssh.invoke_shell()
data = channel.recv(5000)
set_of_commands = ["cmd\n","reset\n"]
while set_of_commands is not None:
file.write(data)
# New command might be executed here if we have 'system:'
if re.match("system:",data) is not None:
try:
command=set_of_commands.pop()
channel.send(command)
except:
break
data=channel.recv(5000)
【问题讨论】:
-
ssh.connect API 中使用的超时用于 TCP 连接超时。
-
你试过使用 channel.recv_exit_status() 吗?