Python Wake On Lan
2019-11-17 本文已影响0人
royplusone
Python Wake On Lan 的脚本
import socket
import re
# How-to
# >> python wol.py 00-00-00-00-00-C4
# Get Mac from input
# mac_ext = "00-00-00-00-00-C4" # For mobile
mac_ext = sys.argv[1]
# Support format 00:00:00:00:00:00 or 00-00-00-00-00-00
mac = ''.join(re.findall('[^-:]',mac_ext)) # match character w/o '-' and ':'
# Transform to magic packet
data = ''.join(['FFFFFFFFFFFF', mac * 16]) # Magic packet string
send_data = bytes.fromhex(data) # String to bytes
# Broadcast via socket
destination = ('255.255.255.255', 9)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.sendto(send_data, destination)
# print("WOL packet %d bytes sent !" % len(send_data))
注意
很多主板WOL不支持断电启动,非法关机后WOL无法唤醒。资料说正常关机后会在网卡设置标志位,掉电后这个标志位丢掉了故无法唤醒
玩法
- 同一内网中手机唤醒,IOS可以使用App Pythonista,执行Python脚本,调整下代码mac_ext写死。
- Shell 中执行 python wol.py 00-00-00-00-00-C4
- 下一步准备将脚本放到树莓派上,外网远程唤醒