본문 바로가기

'.' Programs/PHP

PHP 에서 Magic Packet (WOL : 컴퓨터 원격 부팅) 을 보내는 소스.

선행 조건 

컴퓨터 바이오스에서 Wake On Lan 옵션이 설정 되어 있어야 한다.


001<?php

002# Wake on LAN - (c) HotKey@spr.at, upgraded by Murzik
003# Modified by Allan Barizo http://www.hackernotcracker.com
004// Reformatted, improved and OO'ized by Chris Seto http://www.chrisseto.com
005 
006class WOL
007{
008    private $broadcast   '';
009    private $lastStatus  'NA';
010 
011    /**
012     * void __construct(string $network)
013     *  Start the WOL class and specify broadcast network
014     *
015     * @param $network              The network to broadcast on
016     * @return void
017     */
018    public function __construct($network)
019    {
020        // TODO: Verification
021        $this->broadcast = $network.'.255';
022    }
023 
024    /**
025     * string getLastStatus(void)
026     *  Get status of last wake up call
027     *
028     * @return string           Result of last wakeOnLan
029     */
030    public function getLastStatus()
031    {
032        return $this->lastStatus;
033    }
034 
035    /**
036     * bool wakeOnLan(string $mac, int $socket_number)
037     *  Wake up a remote computer
038     *
039     * @param $mac              The MAC address of the remote computer to wake
040     * @param $socket_number    The socket to wake on, usually port 7
041     * @return true/false
042     */
043    public function wakeOnLan($mac$socket_number=7)
044    {
045        $addr_byte explode(':'$mac);
046        $hw_addr   '';
047 
048        for ($a=0; $a <6; $a++)
049            $hw_addr .= chr(hexdec($addr_byte[$a]));
050 
051        $msg chr(255).chr(255).chr(255).chr(255).chr(255).chr(255);
052 
053        for ($a = 1; $a <= 16; $a++)
054            $msg .= $hw_addr;
055 
056        // Try to create socket
057        if (!$s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP))
058        {
059            $this->lastStatus = 'socket_create_fail';
060            return false;
061        }
062 
063        // Try to set broadcast option
064        if (socket_set_option($s, 1, 6, TRUE) < 0)
065        {
066            $this->lastStatus = 'setsockopt_fail';
067            return false;
068        }
069 
070        // Try to send magic packet
071        if (socket_sendto($s$msgstrlen($msg), 0, $this->broadcast, $socket_number))
072        {
073            $this->lastStatus = 'OK';
074            socket_close($s);
075            return true;
076        }
077        else
078        {
079            $this->lastStatus = 'send_fail';
080            return false;
081        }
082    }
083}
084 
085// Create the WOL object
086$wol new WOL('192.168.15');
087 
088// Wake up!
089$wol->wakeOnLan('00:XX:XX:51:XX:FC');
090 
091// Handle the status for the last call to WakeOnLan()
092switch ($wol->getLastStatus())
093{
094    case 'OK':
095    echo 'Magic packet sent!';
096    break;
097 
098    case 'NA':
099    echo 'No call to wakeOnLan() has been made...';
100    break;
101 
102    case 'socket_create_fail':
103    echo 'Could not create socket!';
104    break;
105 
106    case 'setsockopt_fail':
107    echo 'Could not set socket option!';
108    break;
109 
110    case 'send_fail':
111    echo 'Could not send packet!';
112    break;
113 
114    default:
115    echo 'I have no idea what happened.';
116    break;
117}