Skip to content

Instantly share code, notes, and snippets.

@tassoevan
Last active August 29, 2015 14:27
Show Gist options
  • Save tassoevan/0f2d5c5b4854d4505b64 to your computer and use it in GitHub Desktop.
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.
<?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