[英]python paramiko timeout for commands
我需要有关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)
您可能在下面使用每个命令都有超时时间。 我已经从这篇文章中获得了参考。 Python Paramiko超时,执行时间长,需要完整输出
chan = ssh.get_transport().open_session()
cmd = "timeout {0} {1}\n".format(timeouttime, cmd)
chan.exec_command(cmd)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.