如何重新启动远程服务器并等待设备再次启动并在重新启动后使用 paramiko python 执行另一个命令

how to reboot the remote server and wait for device to up again and execute another commands after reboot using paramiko python

我试过以下代码:

client = paramiko.SSHClient()        
client.load_system_host_keys() 

chan = client.get_transport().open_session(timeout=timeout)

chan.settimeout(timeout) 

chan.exec_command("/sbin/reboot -f > /dev/null 2>&1 &") 

这段代码只是让重启在后台执行,重启后切换到另一个命令。 但要求是远程服务器应该重新启动,并且从客户端代码应该等到远程服务器再次启动,然后在重新启动语句后执行另一个命令。

你可以试试

while True:
    try:
        # your commands
        break
    except:
        sleep(some_timeout)

我还建议使用 fabric:它有一些不错的功能,包括命令的重试参数。它在 fabric.api.

中也有 reboot 命令