一、远程重启对端机器
1、使用cmd即可重启对端机器
前提:
本机与对端机器在同一局域网中
重启命令:
shutdown -r -f -t 1 -m \\10.58.57.61
参数详解:
-r 重启
-s 关闭
-f 关闭正在运行的程序
-t 多久执行 上面 设置 1 秒
-m 远程关闭电脑的 IP 地址
-a 终止关闭
-c “里面写为什么关机 或者重启的注释” 关机或者重启的注释
2、使用python实现以上操作
import subprocess
import logging
import time
# 在cmd远程控制电脑重启
ip = "10.58.57.61"
cmd = r"shutdown -r -f -t 1 -m \\" + ip
subprocess.run(cmd, timeout=60) # 60s未执行完就会报错
logging.info("远程机器重启中,请稍后...")
time.sleep(100)
# 循环检测以下是否能ping通
icount = 0
while icount < 5:
try:
cmd_ping = r"ping " + ip
result = subprocess.run(cmd_ping, capture_output=True, timeout=60)
result = result.stdout.decode('gbk') # 注意此处要解码,不然结果不是字符串型
logging.info(result)
if "100%丢失" not in result:
logging.info('ping通,重启后网络已恢复')
break
except Exception as e:
logging.info("error: {}".format(e))
icount += 1
logging.info('ping不通,等待下一次尝试')
time.sleep(30)
二、命令行选择并复制本地文件
import subprocess
# 使用命令行选择并复制文件
local_file = r'D:\word\file.docx'
cmd = ['powershell', 'Get-Item "%s" | Set-Clipboard' % local_file]
result = subprocess.run(cmd, timeout=10).returncode
assert result == 0 # 判断为0说明复制成功