forked from mritd/shell_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwol.py
executable file
·46 lines (34 loc) · 1.06 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
#!/usr/bin/env python
#coding=utf-8
import socket, sys
import struct
def to_hex_int(s):
return int(s.upper(), 16)
dest = ('192.168.1.255', 9)
if len(sys.argv) < 2:
print("usage: %s <MAC Address to wakeup>" % sys.argv[0])
sys.exit()
mac = sys.argv[1]
spliter = ""
if mac.count(":") == 5: spliter = ":"
if mac.count("-") == 5: spliter = "-"
if spliter == "":
print("MAC address should be like XX:XX:XX:XX:XX:XX / XX-XX-XX-XX-XX-XX")
sys.exit()
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)
print("WOL packet %d bytes sent !" % len(packet))