Simple WoL script
Simple wake-on-lan script, to wake up a WoL enabled computer when it receives a connection. This script is currently running on an old router.
It requires the wol command.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
#!/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