远程开机脚本

以前用笔记本都习惯不关机,这样在哪都能随时远程上去。现在换台式机不能那么干了,有点太浪费电。于是写了个脚本,需要用家里电脑时,随时远程开机,这样不用家里电脑一直开着,也挺方便。
代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
#coding=utf-8
# Created Time: 2015-01-23 16:23:02
# Modified Time: 2015-01-23 18:14:34
# Created By: Vic <vic@libgod.com>

from optparse import OptionParser
import socket
import struct

def wake(addr, mac):
mac_data = []
for i in range(0, 12, 2):
mac_data.append(int(mac[i:i+2], 16))
packet = struct.pack("!BBBBBB", 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
packet_mac = struct.pack("!BBBBBB", *mac_data)
for i in range(0, 16):
packet += packet_mac
#print "len: ", len(packet), "data: ", packet
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
try:
s.sendto(packet, addr)
print "唤醒数据包发送完成", addr, mac
finally:
s.close()

def main():
usage = "%prog [options]"
parser = OptionParser(usage = usage)
parser.add_option("-a", "--addr", dest="addr", help="Boardcast address", metavar="255.255.255.255")
parser.add_option("-p", "--port", dest="port", help="Port", metavar="7")
parser.add_option("-m", "--mac", dest="mac", help="MAC address", metavar="FF-FF-FF-FF-FF-FF")
(options, args) = parser.parse_args()
if not options.mac:
parser.print_help()
return
addr = "255.255.255.255"
port = 7
mac = options.mac.replace("-", "")
if options.addr:
addr = options.addr
if len(mac) != 12:
print "无效的MAC地址: %s" % options.mac
return
if options.port:
port = int(options.port)
wake((addr, port), mac)

if __name__ == "__main__":
main()

脚本已经试验可用,如果你不能远程开机,可能是主板禁用了网络远程开机,需要改下BIOS设置,具体设置方法Google一下就有了。
下载地址

文章目录