메뉴 건너뛰기

DDART.NET

PHP로 WOL Magic Packet 보내기

DDART 2018.10.31 00:34 조회 수 : 17

 

<?php
function wol($broadcast, $mac)
{
    if (strstr($mac, "-"))
    {
        $mac_array = explode('-', $mac);
    }
    else
    {
        $mac_array = explode(':', $mac);
    }
    $hwaddr = '';

    foreach($mac_array AS $octet)
    {
        $hwaddr .= chr(hexdec($octet));
    }

    // Create Magic Packet

    $packet = '';
    for ($i = 1; $i <= 6; $i++)
    {
        $packet .= chr(255);
    }

    for ($i = 1; $i <= 16; $i++)
    {
        $packet .= $hwaddr;
    }

    $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
    if ($sock)
    {
        $options = socket_set_option($sock, SOL_SOCKET, SO_BROADCAST, true);

        if ($options >=0) 
        {    
            $e = socket_sendto($sock, $packet, strlen($packet), 0, $broadcast, 7);
            socket_close($sock);
        }    
    }
}
$broadcast_ip = "192.168.1.255";
$mac_addr = "94-D0-80-00-06-5D";

wol($broadcast_ip, $mac_addr);

?>