Apr
19

WOL: Wake-on-LAN Tutorial with Bonus PHP Script

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!"

 

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.

PHP:
  1. <?
  2. # Wake on LAN - (c) HotKey@spr.at, upgraded by Murzik
  3. # Modified by Allan Barizo http://www.hackernotcracker.com
  4. function WakeOnLan($addr, $mac,$socket_number) {
  5.   $addr_byte = explode(':', $mac);
  6.   $hw_addr = '';
  7.   for ($a=0; $a <6; $a++) $hw_addr .= chr(hexdec($addr_byte[$a]));
  8.   $msg = chr(255).chr(255).chr(255).chr(255).chr(255).chr(255);
  9.   for ($a = 1; $a <= 16; $a++) $msg .= $hw_addr;
  10.   // send it to the broadcast address using UDP
  11.   // SQL_BROADCAST option isn't help!!
  12.   $s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
  13.   if ($s == false) {
  14.     echo "Error creating socket!\n";
  15.     echo "Error code is '".socket_last_error($s)."' - " . socket_strerror(socket_last_error($s));
  16.     return FALSE;
  17.     }
  18.   else {
  19.     // setting a broadcast option to socket:
  20.     $opt_ret = socket_set_option($s, 1, 6, TRUE);
  21.     if($opt_ret <0) {
  22.       echo "setsockopt() failed, error: " . strerror($opt_ret) . "\n";
  23.       return FALSE;
  24.       }
  25.     if(socket_sendto($s, $msg, strlen($msg), 0, $addr, $socket_number)) {
  26.       echo "Magic Packet sent successfully!";
  27.       socket_close($s);
  28.       return TRUE;
  29.       }
  30.     else {
  31.       echo "Magic packet failed!";
  32.       return FALSE;
  33.       }
  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.  
  44. WakeOnLan($ip_addy, $mac_addy,$socket_number)
  45.  
  46.  
  47. ?>

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!

Update: Welcome to all of the digg.com users! Thanks for digging and for coming to my site.





Additional Reading

Comment View Comments from Other Readers

Popular Posts

Featured Posts

Related Posts

Recent Posts

What's Your Reaction?


Subscribe to this Blog:

Recent Reactions Elsewhere


 

35 Responses to “WOL: Wake-on-LAN Tutorial with Bonus PHP Script”

  1. samopal Says:

    hi, i tried this one, but for me it doesnt work.

    when i save it as .php (there is missing "

  2. hacker not cracker Says:

    okay i found the problem. on line 8, make sure the quotes are straight not "curly." if neccessary, replace all curly quotes in the code with straight generic ones. i guess its the way my blog platform renders the code onto the browser. sorry!

  3. Hasan Says:

    when i run your code wake on lan, it gives the warning below:

    Warning: socket_set_option() [function.socket-set-option]: unable to set socket option [0]: An invalid argument was supplied. in D:\xampp\htdocs\sigmatech_v1.3\sigmatech\Offshore Software Development\attendence_payroll_v1.3\design\wake-on-lan.php on line 29
    Magic Packet sent (102) to 127.0.0.1

    wht should i do?

    thanks for your tutorials.
    Hasan.

  4. phrackattack Says:

    If you want to fix the bad parameter problem, change TRUE to 1 in the socket_set_option function.

    To be frank with you, you don't code like you know how broadcast packets work. You should only need the MAC address.

    Here's some code I wrote that works perfectly:

  5. phrackattack Says:

    http://phrackattack.net/sources/wakeonlan.txt

  6. phrackattack Says:

    I misspoke when I said to replace TRUE with 1. I meant change the other values to SOL_SOCKET and SO_BROADCAST, respectively.

  7. hacker not cracker Says:

    Like I said, I got some of this code from the PHP website so it isn't entirely mine. This is because coding for sockets is a bit difficult for me to grasp. I'll definatly try the code that your provided. Thank you for your feedback! BTW, cool website. I really love your ASCII art concept!

  8. mahmoud Says:

    kaskdj sjlkasd askjdflkjsa dlakjsd

  9. neil Says:

    nice little script

    [LowLoad.co.uk] - UK Dedicated Servers

  10. B Kearan Says:

    I'm looking to send a WOL packet that has the PC run a command as soon as it starts up... something handy, like, say, Scandisk.

  11. hacker not cracker Says:

    there are many things you can do. assuming that you are using windows, you can do a scheduled task (control panel), a startup process (put a shortcut in the startup folder in the programs folder), add a command to the registry here: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run.

    basically anything that you tell windows to run at startup will eventually by executed by your computer after recieving a magic packet

  12. B Kearan Says:

    I know all of those - and they all require Windows to be running before they work. I was asking if there was any way to run a dos level - like scandisk - using the WOL magic packet to trigger it.

  13. hacker not cracker Says:

    just edit the autoexec.bat file in the C:\ directory. and add the line "scandisk /all"

    everytime before windows starts, it will do a full scandisk. I hope this answers your question! :)

  14. doh! Says:

    "undocumented"? "obscure"? nonsense! autoritative whitepaper - http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/20213.pdf

    port number doesnt really matters for target machine (that 1..50000 range is for shamans) echo 7/udp or *better* discard 9/udp is used to minimize disturbance of other hosts in the target broadcast domain.

    about broadcast vs unicast. unicasting WOL packet probably will not work at all (ARP is down while machine sleeps). for the same broadcast domain
    should send() WOL packet to 255.255.255.255 and in case of different broadcast domain (must have route to there, allowed directed broadcasts, etc)
    should send to broadcast address of target subnet (all 1's in host part)

  15. Shiva Says:

    Here's a freeware that does the same from a Java enabled mobile phone

    http://thinkabdul.com/2007/04/26/j2me-wakeonlan-client-for-java-mobile-phones-wakeupbootuppower-on-computer-remotely-behind-router-from-mobile-device-via-gprsedge3g/

  16. Sasa Spanovic Says:

    If somebody didn't understand what "phrackattack" sad, the line 21: $opt_ret = socket_set_option($s, 1, 6, TRUE);

    should be changed to:
    $opt_ret = socket_set_option($s, SOL_SOCKET, SO_BROADCAST, TRUE);

  17. pisosse Says:

    any way of implementing tis on a website to log on via html/flash page?

  18. Andre Says:

    I don't understand it. How do I use this code (execute it) ?

  19. chandra Says:

    I keep getting Call to undefined function socket_create();

    IIS 6.0(I think)
    PHP 5.2.5
    (on my local machine)
    have all extensions loaded; and also tried enabling extension=php_sockets.dll
    also made sure extension_dir was right and restarted the IIS.

    I am revisiting PHP after a long time & I used the php-5.2.5-win32-installer.msi to install PHP.

    Please advise!

  20. DPC Says:

    The socket socket_create() tells PHP to do exactly that. However inorder to do that PHP has to be configured/compiled with the sockets enabled.

    I am running into an error "socket_sendto() unable to write to socket [1]: Operation not permitted in line 26".

    Operation not permitted?? How do I permit it?

    DPC

  21. Juanjo Bareiro Says:

    This reply goes to Pisosse's question, though coming a little late (6 months later!), I hope that it would be useful to somebody :).

    Copy+paste the code to a php file, set the MAC and IP addresses where you're sending the Packet, and run the php by command-line or by http-request.

    A requirement that I didn't see on ANY forum is that before letting PHP try to send the packet, your OS has to got an entry for the PC's MAC address on it's ARP table. That would be, if you're trying to send the packet to a PC with IP 200.9.5.130 and MAC 01:02:03:04:05:06 from a Linux PC, first you should run the command: "arp -s 200.9.5.130 01:02:03:04:05:06", or otherwise add the following line to the /etc/ethers file: "01:02:03:04:05:06 200.9.5.130", so Linux would be able to locate the MAC address for the host you're trying to talk to.
    Another way I found to send the magic packet without having to explicitly add the ARP-line to your ARP-table or the ethers file, is to send the packet to an IP-broadcast-address on your network (preferably not 255.255.255.255 for the traffic it would generate, but it will do too), so your OS will not be guessing the MAC address before sending the packet, and therefore send it right away.

    Cheers from Paraguay! :) (jjbareiro@gmail.com)

  22. Darryl Says:

    Well done!
    Worked first time "out of the box"

    Time from Google->waking up my PC was less than 5 mins from start to finish.

    Thanks.

  23. Speedos Says:

    Did it under 2min!

  24. CwD Says:

    Great job on this. Works fine on my personal server. However, on a shared sever I also get the
    socket_sendto() "unable to write to socket [1]: Operation not permitted" error. Any way around this?

    I know depiscus has a COM / dll extension for php that supposedly works on Windows servers, but I can't figure that one out either. Seems more complicated and rather large (300kb) just to send a packet . . . http://www.depicus.com/wake-on-lan/wake-on-lan-com.aspx

  25. Salman Sheikh Says:

    How can I set up WOL?

  26. rodrigot Says:

    since a turned-off doesn't have an IP, how can u reach it?

  27. Tomás Says:

    Excelent script but I've a problem to use

    Warning: socket_sendto() unable to write to socket [1]: Operation not permitted

    How can i fix it?

    See you,
    Tomás form Argentina

  28. hacker not cracker Says:

    You use the broadcast address (example 192.168.1.255) which will be sent to all nodes in a subnet. Basically, use the network prefix (192.168.1) with ".255" appended.

  29. hacker not cracker Says:

    Tomas, your hosting provider either blocked that function or blocked that port. Contact them to open the function and the port you plan to use.

  30. bob Says:

    my target machine sits behind a router, like most machines, right?. the router folks say its always illegal to use addr ending in .255 like in forwarding a magic packet traffic to port 9, etc. - i have tried with dlink 624I and linksys wrt150n. these are pretty common for home or small biz use. i'm missing something here? how do you get the magic packets addressed to x.x.x.255 if no routers will alllow a pinhole/port fwd configuration using that address ??

  31. Chris Says:

    On my (I think default) install of php under xp the sockets extension wasn't enabled. It worked great once I figured it out. Thanks!

  32. Chepe Says:

    Hi, just want to know what if I dont want to Power Up a pc, lets say I want to Power Off my computer, so whats the magic packet for this?, thanks

  33. Vanishing Son Says:

    Hi there nice post but i have 2 Questions and one problem
    1st : It didn`t work with me at all i tryed everything u said and nothing work.
    2nd : my pc behind router and i want to wake them up from my home
    so how i get the internet IP Coz when when i get it if i restart the router or the elc get off and back again the internet IP will change
    and if i sent the broadcast as u said
    I know that my company that give me the DSL line use the same broadcast to every member it have
    so when i sent the magic packet it will search all over the internet to find the right mac add???
    Please help me here
    P.S Sorry for my english and Sorry coz i`m noob :(

  34. Jan Says:

    -include sockets PHP extension in PHP.ini:
    extension=php_sockets.dll
    -be sure to have shorttags enabled in PHP.ini:
    short_open_tag = On

    -change line
    $opt_ret = socket_set_option($s, 1, 6, TRUE);
    into
    $opt_ret = socket_set_option($s, SOL_SOCKET , SO_BROADCAST, 1);

    -forward UDP port 9 in your router

    and it works like a charm! Thanks!

  35. Jan Says:

    An update:
    It only worked for some hours.

    First I tried to forward the port to .255 as suggested at the end of the article. Tomato firmware doesn\\\\\\\\\\\\\\\'t allow this.

    Then I found a new way:
    1) Pick an unused IP address for your LAN (I used 192.168.1.250)
    2) Set up a port forward form a port of your choice to this address
    3) Add a static ARP entry that maps this address to a broadcast address by adding the following line e.g. for the Tomato firmware it\\\\\\\\\\\\\\\'s under Administration/Scripts/Firewall:

    ip neighbor add 192.168.1.250 lladdr ff:ff:ff:ff:ff:ff dev br0 nud permanent

    Now it works always! Credits go to http://www.perculasoft.com/sleepover/routers.html

Leave a Reply

This is a captcha-picture. It is used to prevent mass-access by robots. (see: www.captcha.net)

You must read and type the 4 chars within 0..9 and A..F, and submit the form.

  

Oh no, I cannot read this. Please, generate a

 
Latest Post on Loading...: Please Wait...
admin admin
© 2006 and web design of Allan Ray Barizo from [art] [⁄app].
This site is best viewed with FF and at least 1024x768 resolution.