Newer update: a much better script over at GitHub can be used on a local web server, such as a Raspberry Pi. It uses a local file as simple storage so that you can add a list of local machines. Then, when you run the script, you can just select the machine you want to wake. The script will then poll the machine until it detects that it is up and will then update the status of that machine in the UI to reflect that. Screen shot below.
Download the script from Github.
Update: I recently moved the REST service described in this post over to a Raspberry Pi where it works really well, and consumes far less power. Just make sure you harden apache and also your headers as per the posts here.
Trying to perform remote Wake On Lan (WOL) to multiple machines over the web can be difficult without an expensive router. Some home routers (including the newer BT Smart Hub) won’t do any forwarding with the WOL ‘magic-packet’. Others, like the older BT Home Hubs, only supports forwarding the ‘magic-packet’ used for WOL on port 9 to one machine on your network, so if you want to wake others from outside your network, you often need to log in to an existing powered-on machine and then run a program within the local network to wake the other machine(s) over the LAN. Not so with this script.
Below is a PHP script I use on my server (it‘s a was a low-power, intel atom server which I leave running) which offers a simple rest service that would allow the server side php script, when requested by a browser, to request a WOL packet to be sent to another machine on the local network in order to wake it up. So, you can wake any machine on your network from anywhere with an internet connection. I you have more than one machine then just create different rest endpoints each with it’s own php script. The more savvy of you could also build a web page UI over the top of this service in order to make it more user friendly.
Prerequisites
- A web server on the local network (I run this on a raspberry pi)
- A router with port forwarding to forward the incoming requests to the web server
- Computers that are capable (and configured) to wake on network access / WOL
Steps
- Add the code below into a .php file on a obscure directory with a long and complicated file name on your web server. Something like
‘/fnjeber3rh jk345t3443nl/nameofcomputertowakeup.php’
Then write this down and create a book mark on your mobile device so that you can easily call it back later. The point of this step is to prevent any accidental switching on your machine – as it stands, any traffic that requests the script address will trigger a WOL request to the target machine. - Update the variables at the bottom of the PHP script to reference the Mac and IP addresses of the target machine and the port number of the magic packet (typically 7 or 9).
- Save the php script
- You may need to modify permissions on the php script
- Setup port forwarding on your router to point incoming internet traffic to the endpoint in your local network
- Navigate to the address using a mobile device – this will call the script and send the magic packet. If everything is working the computer should wake
- If not, then the trouble shooting steps for the obvious failure modes here are: try a different magic packet port number,, check the Mac and IP addresses, check your port forward settings on your router, and check permissions for the php file.
flush();
function WakeOnLan($addr, $mac,$socket_number) {
$addr_byte = explode(':', $mac);
$hw_addr = '';
for ($a=0; $a <6; $a++) $hw_addr .= chr(hexdec($addr_byte[$a]));
$msg = chr(255).chr(255).chr(255).chr(255).chr(255).chr(255);
for ($a = 1; $a <= 16; $a++) $msg .= $hw_addr;
// send it to the broadcast address using UDP
// SQL_BROADCAST option isn't help!!
$s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
if ($s == false) {
echo "Error creating socket!\n";
echo "Error code is '".socket_last_error($s)."' - " . socket_strerror(socket_last_error($s));
return FALSE;
}
else {
// setting a broadcast option to socket:
$opt_ret = socket_set_option($s, 1, 6, TRUE);
if($opt_ret <0) {
echo "setsockopt() failed, error: " . strerror($opt_ret) . "\n";
return FALSE;
}
if(socket_sendto($s, $msg, strlen($msg), 0, $addr, $socket_number)) {
echo "Magic Packet sent successfully!";
socket_close($s);
return TRUE;
}
else {
echo "Magic packet failed!";
return FALSE;
}
}
}
// Port number where the computer is listening. Usually, any number between 1-50000 will do. Normally people choose 7 or 9.
$socket_number = "9";
// MAC Address of the listening computer's network device
$mac_addy = "11:11:11:11:11:11";
// IP address of the listening computer. Input the domain name if you are using a hostname (like when under Dynamic DNS/IP)
//$ip_addy = gethostbyname("myhomeserver.dynamicdns.org");
$ip_addy = '192.168.1.1';
WakeOnLan($ip_addy, $mac_addy,$socket_number);
Due credit for the script must go to [this site].
Leave a Reply