Simple Centralized WakeOnLAN Service

Some of our centralized services (nightly backups, nightly updates of development profiles with latest databases and files) require waking machines over LAN (WOL). Here is a simple PHP script providing this functionality. Usage – just send request to

http://wol.example.com/index.php?machine=HOSTNAME

"User 1", "mac" => "xx:xx:xx:xx:xx:xx");
$machines["charon"] = array("user" => "User 2", "mac" => "xx:xx:xx:xx:xx:xx");
$machines["athena"] = array("user" => "User 3", "mac" => "xx:xx:xx:xx:xx:xx");

$internal_broadcast = "192.168.1.255";
/****** END OF CONFIG *****/

flush();
function WakeOnLan($addr, $mac,$socket_number, $lmachine, $lip) {
$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 to machine $lmachine, IP $lip!"; socket_close($s); return TRUE; } else { echo "Magic packet failed!"; return FALSE; } } } import_request_variables('G'); if (isset($machine)) { // Port number where the computer is listening. Usually, any number between 1-50000 will do. Normally people choose 7 or 9. $socket_number = "7"; $ip = gethostbyname($machine); if ($ip == $machine) { echo "ERROR - Could not resolve machine $machine to IP address, exiting"; exit(); } $found = false; // finding relevant record - basically finding key in the array machines foreach ($machines as $hostname=>$info) {
if ($ip == gethostbyname($hostname)) {
// rewriting the original machine var with "normalized" form
$machine = $hostname;
$found = true;
break;
}
}
if (! $found) {
echo "ERROR - Unknown machine $machine to this tool, please update the machines config accordingly, exiting";
exit();
}

$mac_addy = $machines[$machine]["mac"];
if ($mac_addy == NULL) {
echo "ERROR - Undefined MAC address for machine $machine, IP $ip, exiting";
exit();
}
// 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");
// broadcast to the internal network - in order to choose the correct eth interface
$ip_addy = $internal_broadcast;
if (! WakeOnLan($ip_addy, $mac_addy,$socket_number, $machine, $ip)) {
echo "ERROR - could not WOL";
exit();
}
}
else {
// no parameter, simply listing machines
echo "

Turn on PC over Wake On Lan

";
foreach ($machines as $hostname=>$info) {
echo "$hostname - " . $info["user"] . "
";
}
}
?>

This entry was posted in IT Infrastructure. Bookmark the permalink.
Tags , ,

Comments are closed.