Option for SSH to timeout after a short time? Clie

2019-06-10 08:20发布

I'm sending a command by SSH. This particular command happens to tell the machine to reboot. Unfortunately this hangs my SSH session and does not return so that my script is unable to continue forwards onto other tasks.

I've tried various combinations of modifying the command itself to include "exit" and or escape commands but in none of those cases does the machine pick up on both the reboot and the command to close the SSH session. I've also tried ConnectTimeout and ClientAlive options for SSH but they seem to make the restart command ignored.

Is there some obvious command that I'm missing here?

2条回答
家丑人穷心不美
2楼-- · 2019-06-10 08:53

Well nobody has posted any answers that pertain to SSH specifically, so I'll propose a SIGALRM solution like here: 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)

This basically sets a timer for 30 seconds, then tries to execute your code. If it fails to complete before time runs out, a SIGALRM is sent, which we catch and turn into a TimeoutException. That forces you to the except block, where your program can continue.

查看更多
\"骚年 ilove
3楼-- · 2019-06-10 09:12

Just gonna throw some psuedo-code out there but I would suggest using a flag to denote whether or not you have already rebooted.

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
查看更多
登录 后发表回答