Wake On LAN
And here is a short Python script for a change. It can be used to wake up computers on your LAN. With this script you no longer have to keep your powerful, jet power hungry, computers on 24/7 if you only need them occasionally. Even when you're not at home to push the ON button, the Raspberry Pi can do that for you.
Almost all modern computers can be powered up by sending a so called magic packet over the LAN network to them. For that the computer must be connected with an ethernet cable. Most computers also have some BIOS setting and maybe even a network card setting which should be set to enable Wake On LAN, otherwise it won't work. But usually, on most computers it will work out of the box.
Wireless connected computers can't be powered up over the network though. Neither can your Raspberry Pi itself, but that's hardly an issue knowing the small amount of power it requires anyway.
You could call the script at a pre-set time to power up your computer just before you get out of bed, so it will be ready when you want to check your mail. Or you could switch on a computer on some special events, which triggers the script on your Raspberry Pi. And you can switch on a computer at home, using an ssh session, to get to a particular file which is on your humongous hard disk somewhere.
No matter what you want to use it for, now you can. Uhm, I mean your Raspberry Pi can.
#! /usr/bin/python
# wol
#
# Wake on LAN
#
# Use:
# wol computer1
# wol computer1 computer2
# wol 00:11:22:33:44:55
import sys, struct, socket
# Configuration variables
broadcast = ['192.168.1.255', '192.168.0.255']
wol_port = 9
known_computers = {
'mercury' : '00:1C:55:35:12:BF',
'venus' : '00:1d:39:55:5c:df',
'earth' : '00:10:60:15:97:fb',
'mars' : '00:10:DC:34:B2:87',
}
def WakeOnLan(ethernet_address):
# Construct 6 byte hardware address
add_oct = ethernet_address.split(':')
if len(add_oct) != 6:
print "\n*** Illegal MAC address\n"
print "MAC should be written as 00:11:22:33:44:55\n"
return
hwa = struct.pack('BBBBBB', int(add_oct[0],16),
int(add_oct[1],16),
int(add_oct[2],16),
int(add_oct[3],16),
int(add_oct[4],16),
int(add_oct[5],16))
# Build magic packet
msg = '\xff' * 6 + hwa * 16
# Send packet to broadcast address using UDP port 9
soc = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
soc.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST,1)
for i in broadcast:
soc.sendto(msg,(i,wol_port))
soc.close()
if len(sys.argv) == 1:
print "\n*** No computer given to power up\n"
print "Use: 'wol computername' or 'wol 00:11:22:33:44:55'"
else:
for i in sys.argv:
if i[0] != '/':
if ":" in i:
# Wake up using MAC address
WakeOnLan(i)
else:
# Wake up known computers
if i in known_computers:
WakeOnLan(known_computers[i])
else:
print "\n*** Unknown computer " + i + "\n"
quit()
if len(sys.argv) == 2:
print "\nDone! The computer should be up and running in a short while."
else:
print "\nDone! The computers should be up and running in a short while."
print
There are a couple of configuration variables to set before you can use the script. The variable broadcast must be set to your network's broadcast address. Usually this is only one address, but multiple networks are also supported, as can be seen in the example above. By the way, the broadcast address is the network address with all host bits set to 1. On networks with a netmask of 255.255.255.0 this means that the last byte of the address is always 255.
The variable wol_port can usually left as it is in the listing above.
A list of known computers can be entered in the script in the dictionary called known_computers. Simply name your computers, including the MAC address of their network cards, as shown in the examples in the script.
Please note that Python is rather particular when it comes to leading spaces or TAB characters. To avoid confusion I always use spaces instead of TABs.
Calling the script is simply done by typing its name followed by one or more computers to wake up. Multiple computers should be separated by spaces. You can either name a computer, in which case it must be in the dictionary known_computers, or use the MAC address of the network card of the computer you want to wake up.
example use :
python wol.py earth
Top comments (0)