Last active
August 29, 2015 14:27
-
-
Save tassoevan/0f2d5c5b4854d4505b64 to your computer and use it in GitHub Desktop.
PHP snippet to wake up a machine in network with wake-on-lan feature.
This file contains hidden or 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 | |
$macAddress = '00:1D:7D:84:0E:1E'; // MAC address to be woken up | |
$macAddress = preg_split('/(\\:|\\-)/', $macAddress); | |
if (count($macAddress) != 6) { | |
throw new RuntimeException("Invalid MAC address"); | |
} | |
$macAddress = array_map('hexdec', $macAddress); | |
$macAddress = call_user_func_array('pack', array_merge(['CCCCCC'], $macAddress)); | |
$bytes = pack('CCCCCC', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff) . str_repeat($macAddress, 16); | |
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); | |
socket_set_option($socket, SOL_SOCKET, SO_BROADCAST, 1); | |
socket_sendto($socket, $bytes, strlen($bytes), 0, '255.255.255.255', 9); | |
socket_close($socket); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment