source: misc/wakeup@ 389

Last change on this file since 389 was 372, checked in by Rick van der Zwet, 13 years ago

Usefull when playing around with multiple interfaces.

  • Property svn:executable set to *
File size: 1.6 KB
Line 
1#!/usr/bin/env python
2#
3# Credits: http://code.activestate.com/recipes/358449-wake-on-lan/
4
5import socket
6import struct
7import sys
8
9def wake_on_lan(macaddress, broadcast):
10 """ Switches on remote computers using WOL. """
11
12 # Check macaddress format and try to compensate.
13 if len(macaddress) == 12:
14 pass
15 elif len(macaddress) == 12 + 5:
16 sep = macaddress[2]
17 macaddress = macaddress.replace(sep, '')
18 else:
19 raise ValueError('Incorrect MAC address format')
20
21 # Pad the synchronization stream.
22 data = ''.join(['FFFFFFFFFFFF', macaddress * 20])
23 send_data = ''
24
25 # Split up the hex values and pack.
26 for i in range(0, len(data), 2):
27 send_data = ''.join([send_data,
28 struct.pack('B', int(data[i: i + 2], 16))])
29
30 # Broadcast it to the LAN.
31 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
32 sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
33 sock.sendto(send_data, (broadcast, 7))
34
35
36if __name__ == '__main__':
37 hostmac = { 'brahm' : '00:13:d3:a7:24:51',
38 'hp' : '00:02:e3:3c:c9:3b',
39 'sb' : '00:13:d4:06:0c:79' }
40 if len(sys.argv) == 1:
41 print "Usage: %s <macaddr|host> [<broadcast>]" % sys.argv[0]
42 print ""
43 print "Hosts configured: "
44 for host, mac in hostmac.iteritems():
45 print " - %s : %s" % (host, mac)
46 sys.exit(64)
47
48 try:
49 mac = hostmac[sys.argv[1]]
50 except KeyError:
51 mac = sys.argv[1]
52
53 try:
54 broadcast = sys.argv[2]
55 except IndexError:
56 broadcast = '<broadcast>'
57
58 wake_on_lan(mac, broadcast)
Note: See TracBrowser for help on using the repository browser.