-
Notifications
You must be signed in to change notification settings - Fork 83
/
WOL.py
66 lines (52 loc) · 1.5 KB
/
WOL.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# -*- coding: utf-8-*-
import socket, sys
import struct
import logging
reload(sys)
sys.setdefaultencoding('utf8')
WORDS = ["KAIJI"]
SLUG = "wol"
def Waker(ip,mac):
global sent
def to_hex_int(s):
return int(s.upper(), 16)
dest = (ip, 9)
spliter = ""
if mac.count(":") == 5: spliter = ":"
if mac.count("-") == 5: spliter = "-"
parts = mac.split(spliter)
a1 = to_hex_int(parts[0])
a2 = to_hex_int(parts[1])
a3 = to_hex_int(parts[2])
a4 = to_hex_int(parts[3])
a5 = to_hex_int(parts[4])
a6 = to_hex_int(parts[5])
addr = [a1, a2, a3, a4, a5, a6]
packet = chr(255) + chr(255) + chr(255) + chr(255) + chr(255) + chr(255)
for n in range(0,16):
for a in addr:
packet = packet + chr(a)
packet = packet + chr(0) + chr(0) + chr(0) + chr(0) + chr(0) + chr(0)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET,socket.SO_BROADCAST,1)
s.sendto(packet,dest)
if len(packet) == 108:
sent = True
def handle(text, mic, profile, wxbot=None):
logger = logging.getLogger(__name__)
if SLUG not in profile or \
not profile[SLUG].has_key('ip') or \
not profile[SLUG].has_key('mac'):
mic.say('WOL配置有误,插件使用失败', cache=True)
return
ip = profile[SLUG]['ip']
mac = profile[SLUG]['mac']
try:
Waker(ip,mac)
if sent:
mic.say('启动成功', cache=True)
except Exception, e:
logger.error(e)
mic.say('抱歉,启动失败', cache=True)
def isValid(text):
return any(word in text for word in [u"开机", u"启动电脑", u"开电脑"])