Created
June 27, 2019 22:53
-
-
Save vrelk/b3cd0030cdacf82a35d5044645fadc2d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if(!filter_var($_GET['mac'], FILTER_VALIDATE_MAC)){ | |
//invalid mac | |
die(); | |
} | |
if(!filter_var($_GET['broadcast'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)){ | |
//invalid ipv4 address | |
die(); | |
} | |
wol($_GET['broadcast'], $_GET['mac']); | |
function wol($broadcast, $mac) | |
{ | |
$hwaddr = pack('H*', preg_replace('/[^0-9a-fA-F]/', '', $mac)); | |
// Create Magic Packet | |
$packet = sprintf( | |
'%s%s', | |
str_repeat(chr(255), 6), | |
str_repeat($hwaddr, 16) | |
); | |
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); | |
if ($sock !== false) { | |
$options = socket_set_option($sock, SOL_SOCKET, SO_BROADCAST, true); | |
if ($options !== false) { | |
socket_sendto($sock, $packet, strlen($packet), 0, $broadcast, 7); | |
socket_close($sock); | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment