[288] | 1 | #!/usr/bin/env python
|
---|
| 2 | #
|
---|
| 3 | # Credits: http://code.activestate.com/recipes/358449-wake-on-lan/
|
---|
| 4 |
|
---|
| 5 | import socket
|
---|
| 6 | import struct
|
---|
| 7 | import sys
|
---|
| 8 |
|
---|
| 9 | def wake_on_lan(macaddress):
|
---|
| 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 |
|
---|
| 36 | if __name__ == '__main__':
|
---|
[289] | 37 | hostmac = { 'brahm' : '00:13:d3:a7:24:51',
|
---|
| 38 | 'hp' : '00:02:e3:3c:c9:3b' }
|
---|
[288] | 39 | if len(sys.argv) == 1:
|
---|
| 40 | print "Usage: %s <macaddr|host>" % sys.argv[0]
|
---|
| 41 | print ""
|
---|
| 42 | print "Hosts configured: "
|
---|
| 43 | for host, mac in hostmac.iteritems():
|
---|
| 44 | print " - %s : %s" % (host, mac)
|
---|
| 45 | sys.exit(64)
|
---|
| 46 |
|
---|
| 47 | if hostmac.has_key(sys.argv[1]):
|
---|
| 48 | wake_on_lan(hostmac[sys.argv[1]])
|
---|
| 49 | else:
|
---|
| 50 | wake_on_lan(sys.argv[1])
|
---|