[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
本文目标
远程开机原理
Python 远程开机代码实现
Python 关机说明
Python 开机
关于用程序来开机,是怎么做到的呢?这就是 WOL 技术 Wake-On-Lan,
利用计算机在关机或休眠状态时,网卡及主板部分仍然有微弱的供电,当然要保持电源是插上的,而且是有线连接。所以即使在关机情况下,网卡还是有一定运作能力的,可以监听计算机外部的网上广播信息,当发现特定格式的信息后就会执行开机。现在的主板基本都支持。
只需要在网络适配器属性中配置下面两个选项
0.png
1.png
下图这个勾不能选上,我们要保证网卡在关机后仍有供电,如果你关机后,网口的灯不亮了,就不行了,要保持网口灯亮。
2.png
这个特定数据也有个好听的名字叫 Magic Packet(魔法数据包)。一般用过 UDP 协议进行广播。端口一般是7或者9. 魔法数据包总是以"FF FF
FF FF FF FF" 连续6个 "FF",后面是 MAC 地址信息,一旦网卡侦测到数据包内容,就会唤醒目标计算机。
我们还是来看一下代码怎么写,代码涉及到 UDP 网络编程这块,后面会专门写写网络编程这块内容。此函数只需输入目标计算机的 MAC
地址即可,在局域网中(当然,如果你的计算机有公网IP,那么在公网中也是可以的)另一台计算机上运行此程序,目标计算机即可自动开机。
代码如下,代码本身很短,原理也很简单:
def wake_up(mac='DC-4A-3E-78-3E-0A'):
MAC = mac
BROADCAST = "192.168.0.255"
if len(MAC) != 17:
raise ValueError("MAC address should be set as form 'XX-XX-XX-XX-XX-XX'")
mac_address = MAC.replace("-", '')
data = ''.join(['FFFFFFFFFFFF', mac_address * 20]) # 构造原始数据格式
send_data = b''
# 把原始数据转换为16进制字节数组,
for i in range(0, len(data), 2):
send_data = b''.join([send_data, struct.pack('B', int(data[i: i + 2], 16))])
print(send_data)
# 通过socket广播出去,为避免失败,间隔广播三次
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.sendto(send_data, (BROADCAST, 7))
time.sleep(1)
sock.sendto(send_data, (BROADCAST, 7))
time.sleep(1)
sock.sendto(send_data, (BROADCAST, 7))
print("Done")
except Exception as e:
print(e)
Python 关机
关机来说相对就容易多了,网上也有很多现成的解决方案。大多数是用程序扫描邮箱,如果收到关机相关关键词的邮件,则程序自动执行关机命令。另外如果只是在局域网里实现关机,或者你有公网
IP 地址的话,也可以在本地计算机起一个服务,比如用 Flask 运行在5000端口。向5000这个地址发送指令即可。本机程序中需要有一条执行关机的命令即可
import os
os.system('shutdown -s -t 00')
结尾