我正在编写作为使用 WakeOnLan 功能在网络上打开系统的应用程序。
我用谷歌搜索并能够从这里获取代码。我的代码如下所示。我也在我的路由器中转发了端口 9。
我已经从电源管理中为网卡启用了 LAN 唤醒选项。我按照这里的说明进行操作
我已经从这里安装了 Wake on Lan Monitor/Sniffer,以检查我是否能够接收到唤醒的魔法数据包。并且系统正在接收魔术包。当我从同一网络上的另一个系统(笔记本电脑)关闭并运行 WOL python 脚本时,我的系统无法开机。
任何人都可以建议我解决方案。
我的系统是 Win 8.1 的桌面,需要在 LAN 上唤醒。装有 Win 8 的笔记本电脑,需要运行应用程序并将魔术包发送到桌面。
我的局域网 IP 范围从 172.16.0.1 等,所以使用 172.16.255.255 作为广播地址。
import sys, struct, socket
# Configuration variables
broadcast = ['172.16.255.255']
wol_port = 9
known_computers = {
'mercury' : '00:1C:55:35:12:BF',
'venus' : '00:1d:39:55:5c:df',
'earth' : '00:10:60:15:97:fb',
'mars' : '00:10:DC:34:B2:87',
}
def WakeOnLan(ethernet_address):
# Construct 6 byte hardware address
add_oct = ethernet_address.split(':')
if len(add_oct) != 6:
print "\n*** Illegal MAC address\n"
print "MAC should be written as 00:11:22:33:44:55\n"
return
hwa = struct.pack('BBBBBB', int(add_oct[0],16),
int(add_oct[1],16),
int(add_oct[2],16),
int(add_oct[3],16),
int(add_oct[4],16),
int(add_oct[5],16))
# Build magic packet
msg = '\xff' * 6 + hwa * 16
# Send packet to broadcast address using UDP port 9
soc = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
soc.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST,1)
for i in broadcast:
soc.sendto(msg,(i,wol_port))
soc.close()
def wol(*macs):
if len(macs) == 0:
print "\n*** No computer given to power up\n"
print "Use: 'wol computername' or 'wol 00:11:22:33:44:55'"
else:
for i in macs:
if i[0] != '/':
if ":" in i:
# Wake up using MAC address
WakeOnLan(i)
else:
# Wake up known computers
if i in known_computers:
WakeOnLan(known_computers[i])
else:
print "\n*** Unknown computer " + i + "\n"
quit()
if len(macs) == 2:
print "\nDone! The computer should be up and running in a short while."
else:
print "\nDone! The computers should be up and running in a short while."
print
wol('My System MAC address')