westmood
westmood

Reputation: 9

How to set up a wetsite that based on PHP WOL(wake on lan)

I am an IT Helpdesk with very little PHP experience. Now I have a project in the company to let a colleague connect to the company's intranet using a VPN, and then remotely wake up his computer at the company. So my idea is to set up a PHP-based website in the company, let a colleague enter his computer's MAC address, and then he can wake up his computer remotely after submission. I found the following code on the Internet, but there will be errors when applying it. error message is bleow



Can you help me? Thanks in advance.

wol.php

    <?php
    /**
    * Implement Wake-on-LAN function
    */
    class WOL
    {
        private $hostname;    // The hostname of the wake-up device 
        private $mac;         // The mac address of the wake-up device
        private $port;        // The port of the wake-up device
        private $ip;          // The IP address of the wake-up device (not necessary, the program will automatically obtain the corresponding ip according to $hostname)
     
        private $msg = array(
            0 => "The target machine is already awake.",
            1 => "socket_create execution failed",
            2 => "socket_set_option execution failed",
            3 => "magic packet Sent successfully!",
            4 => "magic packet Failed to send!"
        );
         
        function __construct($hostname,$mac,$port,$ip = false)
        {
            $this->hostname = $hostname;
            $this->mac      = $mac;
            $this->port     = $port;
            if (!$ip)
            {
                $this->ip   = $this->get_ip_from_hostname();
            }
        }
     
        public function wake_on_wan()
        {
            if ($this->is_awake())
            {
                return $this->msg[0]; // If the device is already awake, no other operations will be performed
            }
            else
            {
                $addr_byte = explode(':', $this->mac);
                $hw_addr = '';
                for ($a=0; $a<6; $a++) $hw_addr .= chr(hexdec($addr_byte[$a]));
                $msg = chr(255).chr(255).chr(255).chr(255).chr(255).chr(255);
                for ($a=1; $a<=16; $a++) $msg .= $hw_addr;
                // Send data packets via UDP
                $s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
                 
                if ($s == false)
                {
                    return $this->msg[1]; // socket_create Execution failed
                }
     
                $set_opt = @socket_set_option($s, 1, 6, TRUE);
     
                if ($set_opt < 0)
                {
                    return $this->msg[2]; // socket_set_option Execution failed
                }
     
                $sendto = @socket_sendto($s, $msg, strlen($msg), 0, $this->ip, $this->port);
                 
                if ($sendto)
                {
                    socket_close($s);
                    return $this->msg[3]; // magic packet Sent successfully!
                }
     
                return $this->msg[4]; // magic packet Failed to send!
                 
            }
        }
     
        private function is_awake()
        {
            $awake = @fsockopen($this->ip, 80, $errno, $errstr, 2);
             
            if ($awake)
            {
                fclose($awake);
            }
             
            return $awake;
        }
     
        private function get_ip_from_hostname()
        {
            return gethostbyname($this->hostname);
        }
     
    }
    ?>

index.php

    <?php
    include("wol.php");
     
    $WOL = new WOL("255.255.255.255","38-D5-47-AA-69-A2","9");
    $status = $WOL->wake_on_wan();
     
    echo $status;
    ?>

Upvotes: 0

Views: 770

Answers (1)

eis
eis

Reputation: 53462

See here:

__construct($hostname,$mac,$port,$ip = false)

So you should provide hostname, mac address, port and optionally an ip.

You provide this:

new WOL("255.255.255.255","38-D5-47-AA-69-A2","9");

Which can't be correct. "255.255.255.255" is not a target hostname. Additionally, msbit has a good point in the comment that also MAC address should be something like "38:D5:47:AA:69:A2", not "38-D5-47-AA-69-A2". Port 9 seems to be ok according to wikipedia and used for wake-on-lan.

Things you have to fix here to get this to work with wake-on-lan:

  • wake-on-lan apparently is usually sent as a broadcast. this code relies on target having hostname/ip. if your target has them, you should provide them: otherwise, you'll need to change the code to do a broadcast
  • fix MAC address notation to use semicolons
  • right now is_awake seems to consider a target as awake if it has port 80 listening. does that apply to your use case? if not, you'll need to change that too.

Upvotes: 2

Related Questions