通过 Python 2.7 重启远程 ubuntu 服务器

Rebooting a remote ubuntu server via Python 2.7

谁能告诉我如何通过 Python 重启远程服务器?

我尝试了以下命令:-

os.system('sudo ssh -q -i /home/support/.ssh/id_rsa_vnera_cluster_keypair -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null support@a.b.c.d "sudo -S su ubuntu -c 'sudo reboot --force'"')

但是我的代码卡在上面的命令中等待响应,因为 reboot 命令没有 return 任何东西。

2 条建议:

  1. 尝试在您发送给远程服务器的命令字符串中使用 nohup
  2. 而不是使用 os.system(同步)使用 subprocess.Popen()(可以同步或异步使用),如下所示:
import subprocess as sp

p = sp.Popen(['sudo', 'ssh', '-q' ...])  # complete the list with the rest of your remote command

p.wait()    # if you want to wait.  Can include a timeout in wait().