前言
今天有拷贝笔记本电脑数据和对笔记本电脑的简单操作需求,但同时操作两套键鼠实属麻烦,于是在RDP
的基础上产生了把笔记本电脑放到靠近路由器的位置并保持网线连接以随用随开的美好构想。
实现
首先在路由器后台做一下ARP
绑定,如下图所示:
随后执行Python
脚本:
import socket, sys
import struct, os, time
def wake_up(mac):
MAC = mac
BROADCAST = "192.168.31.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)
# 下面写成需要唤醒电脑的网卡Mac地址
wake_up('XX-XX-XX-XX-XX-XX')
如果运行结果为Done
,则唤醒成功!
总结
如果没记错,这应该是第二篇有关网络唤醒的博文,上一次是通过Siri借助路由器固件实现唤醒的:
参考资料:
https://www.cnblogs.com/gayhub/p/pythonwol.html
https://blog.51cto.com/iplayit/1576048
https://www.cnblogs.com/Alex-Mercer/p/11592192.html
https://blog.csdn.net/hzf16321/article/details/111824120
https://blog.csdn.net/weixin_34083807/article/details/112668907
版权属于:soarli
本文链接:https://blog.soarli.top/archives/581.html
转载时须注明出处及本声明。