jkhsjdhjs

functions.php | WakeOnLAN

May 21st, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.72 KB | None | 0 0
  1. <?php
  2.     function WakeOnLAN($mac_hex, $broadcastAddress) {
  3.         $mac_hex2 = str_replace(':', '', $mac_hex);
  4.         if (!ctype_xdigit($mac_hex2)) {
  5.             echo timestamp();
  6.             echo " <font color=\"red\">MAC Adresse ungültig, nur Ziffern von 0-9 und Buchstaben von a-f sind erlaubt!</font>";
  7.         }
  8.         else {
  9.             $mac_bin = pack('H12', $mac_hex2);
  10.             $magicPacket = str_repeat("\xff", 6) . str_repeat($mac_bin, 16);
  11.             $s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
  12.             if ($s == false) {
  13.                 echo timestamp();
  14.                 echo " <font color=\"red\">Fehler beim Erstellen des Sockets!";
  15.                 echo "<br>" . timestamp();
  16.                 echo " <br>Fehlercode: '".socket_last_error($s)."' - " . socket_strerror(socket_last_errpr($s)) . "</font>";
  17.             }
  18.             else {
  19.                 echo "<br>" . timestamp();
  20.                 echo " <font color=\"lime\">Socket erfolgreich erstellt";
  21.                 $opt_ret = socket_set_option($s, 1, 6, TRUE);
  22.                 if ($opt_ret < 0) {
  23.                     echo ".<br>" . timestamp();
  24.                     echo " <font color=\"red\">setsockopt() fehlgeschlagen, Error: " . strerror($opt_ret) . "</font></font>";
  25.                 }
  26.                 else {
  27.                     echo " und geöffnet.</font>";
  28.                     $e = socket_sendto($s, $magicPacket, strlen($magicPacket), 0, $broadcastAddress, 2050);
  29.                     echo "<br>" . timestamp();
  30.                     echo " Magic Packet an ".$broadcastAddress." mit der MAC=".$mac_hex." gesendet.";
  31.                     socket_close($s);
  32.                     echo "<br>" . timestamp();
  33.                     echo " Socket geschlossen.";
  34.                 }
  35.             }
  36.         }
  37.     }
  38.    
  39.     function ping($ip) {
  40.         $pingresult = exec("/bin/ping -c 1 $ip", $outcome, $status);
  41.         if ($status == 0) {
  42.             return true;
  43.         }
  44.         else {
  45.             return false;
  46.         }
  47.     }
  48.    
  49.     function timestamp() {
  50.         $datum = date("d.m.Y");
  51.         $uhrzeit = date("H:i:s");
  52.         return "<font color=\"blue\">[$datum - $uhrzeit]</font>";
  53.     }
  54. ?>
Advertisement
Add Comment
Please, Sign In to add comment