我正在尝试使用下面的调用来关闭我的系统,但我希望它能在所有主要的操作系统发行版上工作。有一个捕获所有关机命令吗?
import os
os.system("shutdown /s /t 1")有没有其他方法可以通过python代码远程关闭机器?
发布于 2019-11-03 11:55:51
对于远程管理节点,ansible是一个非常好的工具,通过收集事实,你可以获得当前的节点os,然后有条件地相应地关闭。
发布于 2019-11-03 13:46:08
以下函数提供了向远程主机发送命令的便携方式:
def run_shell_remote_command(remote_host, remote_cmd, pem_file=None, ignore_errors=False):
remote_cmd = remote_cmd.split(' ')
cmd = [SSH_PATH, '-o ConnectTimeout=30', '-o BatchMode=yes', '-o StrictHostKeyChecking=no']
if pem_file:
cmd.extend(['-i', pem_file])
cmd.extend([remote_host] + remote_cmd)
print(f"SSH CMD: {cmd}")
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
if p.returncode != 0:
if not ignore_errors:
raise RuntimeError("%r failed, status code %s stdout %r stderr %r" % (
remote_cmd, p.returncode, stdout, stderr))
return stdout.strip() # This is the stdout from the shell command这样,您就可以在远程主机上运行远程操作系统支持任何命令。
https://stackoverflow.com/questions/58676321
复制相似问题