Wake-on-LAN Tutorial with PHP

I love tweaking my computer! Every opportunity that allows me to specify what I want makes me excited of the possibilities. (With the exception of Azureus. That’s just too confusing for me!) In fact, the first thing I check after installing a new program is the possible existence of "Options," "Preferences," "Tools," "Configuration," "Settings," or "Setup!"

cdl_capture_2011-06-23-01_ 000

BIOS (or CMOS)

When I first became involved with computers, I started to mess around with the BIOS. It was an intriguing little 16-color control panel. I’m talking about those settings that you can change after pressing "F1," "F2," or "Delete" before the operating system starts to load.

post_screen.jpg

Mysterious Wake on LAN Setting

bios_screen.jpgI pretty much understood all the settings and what each one did except for this strange "Remote Wake-on-LAN." It wasn’t until much later I discovered that initiating this option allows the computer to boot upon command from the network. Personally, that is ultra cool! But at the time, I really didn’t have a use for it. Then came college.

College Needs

I increasingly saw a need to get a laptop. There were countless times when I needed to access my computer while on campus. Sadly, the only computers that I could use were the ones in the computer labs. I came up with a nifty idea that wouldn’t cost a dime. Since I can’t leave my computer on all day, why don’t I use the Wake-on-LAN function to turn it on remotely and then implement a remote access setup. It would enable me to access my computer from anywhere regardless of whether it was off or on!

How it Works

Wake-on-LAN gives me the impression of an undocumented obscure little function that hardly anyone makes use of. Really, it is so simple that the lack of documentation is understandable. Essentially, enabling this function allows the network device to stay half-awake while the rest of the computer is off. It consumes a miniscule amount of electricity while constantly listening to the network. The network device waits until it receives a "magic packet" (seriously!). A magic packet is a small bundle of data consisting of the bytes "FF FF FF FF FF FF" followed by 16 repetitions of the listening network device’s MAC address.

Creating the "Magic Packet"

A magic packet is fairly easy to generate. Depicus provides great freeware tools that allow you to input the parameters. Personally, I PHP programmed my server to generate the packet. I found some useful PHP WOL code from the PHP website. I modified it so that it will fit my liking. Just change the lines in the end.

PLAIN TEXT – CLICK TO REMOVE NUMBERS

PHP:

  1. <?

  2. # Wake on LAN – (c) HotKey@spr.at, upgraded by Murzik

  3. # Modified by Allan Barizo http://www.hackernotcracker.com

  4. flush();

  5. function WakeOnLan($addr, $mac,$socket_number) {

  6. $addr_byte = explode(‘:’, $mac);

  7. $hw_addr = ”;

  8. for ($a=0; $a <6; $a++) $hw_addr .= chr(hexdec($addr_byte[$a]));

  9. $msg = chr(255).chr(255).chr(255).chr(255).chr(255).chr(255);

  10. for ($a = 1; $a <= 16; $a++) $msg .= $hw_addr;

  11. // send it to the broadcast address using UDP

  12. // SQL_BROADCAST option isn’t help!!

  13. $s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);

  14. if ($s == false) {

  15. echo "Error creating socket!\n";

  16. echo "Error code is ‘".socket_last_error($s)."’ – " . socket_strerror(socket_last_error($s));

  17. return FALSE;

  18. }

  19. else {

  20. // setting a broadcast option to socket:

  21. $opt_ret = socket_set_option($s, 1, 6, TRUE);

  22. if($opt_ret <0) {

  23. echo "setsockopt() failed, error: " . strerror($opt_ret) . "\n";

  24. return FALSE;

  25. }

  26. if(socket_sendto($s, $msg, strlen($msg), 0, $addr, $socket_number)) {

  27. echo "Magic Packet sent successfully!";

  28. socket_close($s);

  29. return TRUE;

  30. }

  31. else {

  32. echo "Magic packet failed!";

  33. return FALSE;

  34. }

  35. }

  36. }

  37. // Port number where the computer is listening. Usually, any number between 1-50000 will do. Normally people choose 7 or 9.

  38. $socket_number = "7";

  39. // MAC Address of the listening computer’s network device

  40. $mac_addy = "00:12:4G:SF:12:13";

  41. // IP address of the listening computer. Input the domain name if you are using a hostname (like when under Dynamic DNS/IP)

  42. $ip_addy = gethostbyname("myhomeserver.dynamicdns.org");

  43. WakeOnLan($ip_addy, $mac_addy,$socket_number)

  44. ?>

Common Hurdles

There are a couple of common problems computer users come across when trying to implement Wake-on-LAN.

  • Physical Network Layer – To my knowledge, WOL will only work with Ethernet. Sorry! No Wi-fi! (Though, I heard that there are some very rare exceptions!)
  • Incompatible BIOS or network device – Sometimes the BIOS or the Ethernet Device will not support WOL. These are the two main components of Wake-on-LAN. The Ethernet detects the "magic packet" and the BIOS turns the computer on.
  • Network Infrastructure – In order for WOL to work, the "magic packet" must go through a direct, IP-address-specific route. That means if there are any routers or if your computers on the network share a public IP address then you’ll need to do some port mapping when send magic packet via the Internet.

Last Tips

If you’re still having trouble with WOL, try sending the magic packet to the broadcast address of the network that the listening computer is a part of. The broadcast address is the IP address prefix with 255 appended at the end. Mine is 192.168.0.255. (Just a little FYI: Everything that is directed to the broadcast address goes to every computer on the network.) Good luck!

 

http://www.hackernotcracker.com/2006-04/wol-wake-on-lan-tutorial-with-bonus-php-script.html

http://wolviaphp.sourceforge.net/

http://forum.synology.com/enu/viewtopic.php?t=2661

Leave a Reply