#!/usr/bin/php -q
<?php
if (!function_exists('pcntl_fork')) die('PCNTL functions not available on this PHP installation');
declare(ticks=1);
//Configuration!
$addressBind = '192.168.1.5';
$portBind = 10066;
$wolEnabledIP = '192.168.1.3';
$wolEnabledNetwork = '192.168.1.255';
$wolEnabledMAC = '00:12:34:56:78:90';
//End-Configuration!
$pid = pcntl_fork();
if ($pid == -1) {
die('could not fork');
} else if ($pid) {
exit;
} else {
//we are the child
}
if (posix_setsid() == -1) {
die("could not detach from terminal");
}
//Write pid
$posid=posix_getpid();
$fp = fopen("/var/run/wol-daemon.pid", "w");
fwrite($fp, $posid);
fclose($fp);
//Signal handlers
pcntl_signal(SIGTERM, "sig_handler");
pcntl_signal(SIGHUP, "sig_handler");
function sig_handler($signo) {
switch ($signo) {
case SIGTERM:
exit;
break;
}
}
/* Allow the script to hang around waiting for connections. */
set_time_limit(0);
//Turn on implicit output flushing so we see what we're getting as it comes in.
ob_implicit_flush();
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}
if (socket_bind($sock, $addressBind, $portBind) === false) {
echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
if (socket_listen($sock, 5) === false) {
echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
do {
if (($msgsock = socket_accept($sock)) === false) {
echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
break;
}
$fp = @fsockopen($wolEnabledIP, 139, $errno, $errstr, 1); //The WoL enabled computer is running Windows, that's why we are testing on port 139.
if (!$fp) {
$msg = "\nSince Celina is not up, I will try to wake her up for you\nPlease wait...\n";
socket_write($msgsock, $msg, strlen($msg));
pclose(popen("wol -i '.$wolEnabledNetwork.' -p 7 ".$wolEnabledMAC, "r"));
while(true) {
sleep(1);
$fpC = @fsockopen($wolEnabledIP, 139, $errno, $errstr, 1);
if (!$fpC) {
continue;
} else {
$msg = "Celina is up and spinning, feel free get the hell out! ;-)\n";
socket_write($msgsock, $msg, strlen($msg));
break;
}
}
} else {
$msg = "\nCelina is up, so what is your point?\n";
socket_write($msgsock, $msg, strlen($msg));
fclose($fp);
}
socket_write($msgsock, $msg, strlen($msg));
socket_close($msgsock);
} while (true);
socket_close($sock);
Reply