SSH在短时间后超时? ClientAlive&ConnectTimeout似乎没有做我需要他们做的事情

我正在通过SSH发送一个命令。 这个特定的命令恰好告诉机器重启。 不幸的是,这会挂起我的SSH会话,并不会返回,所以我的脚本无法继续转发到其他任务。

我已经尝试过修改命令本身的各种组合,以包括“exit”和or escape命令,但是在这些情况下,机器都不会在重新启动和closuresSSH会话的命令中使用。 我也尝试了SSH的ConnectTimeout和ClientAlive选项,但他们似乎使重新启动命令被忽略。

是否有一些明显的命令,我在这里失踪?

那么没有人发布任何有关SSH的答案,所以我会提出一个像这样的SIGALRM解决方案: https : //stackoverflow.com/a/1191537/3803152

 class TimeoutException(Exception): # Custom exception class pass def TimeoutHandler(signum, frame): # Custom signal handler raise TimeoutException # Change the behavior of SIGALRM OriginalHandler = signal.signal(signal.SIGALRM,TimeoutHandler) # Start the timer. Once 30 seconds are over, a SIGALRM signal is sent. signal.alarm(30) # This try/except loop ensures that you'll catch TimeoutException when it's sent. try: ssh_foo(bar) # Whatever your SSH command is that hangs. except TimeoutException: print "SSH command timed out." # Reset the alarm stuff. signal.alarm(0) signal.signal(signal.SIGALRM,OriginalHandler) 

这基本上设置一个计时器30秒,然后尝试执行您的代码。 如果在时间耗尽之前未能完成,则发送SIGALRM ,我们将其捕获并转换为TimeoutException 。 这迫使你到except块,你的程序可以继续。

只是要抛出一些伪代码,但我会建议使用一个标志来表示是否已经重新启动。

rebootState.py

 hasRebooted = False 

sshCommander.py

 from rebootState import hasRebooted if (hasRebooted): # write to rebootState.py "hasRebooted = True" # system call to reboot machine else: # continue execution