0

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


  • Warning: Undefined array key 1 in C:\xampp\htdocs\wol\wol.php on line 41
  • Warning: Undefined array key 2 in C:\xampp\htdocs\wol\wol.php on line 41
  • Warning: Undefined array key 3 in C:\xampp\htdocs\wol\wol.php on line 41
  • Warning: Undefined array key 4 in C:\xampp\htdocs\wol\wol.php on line 41
  • Warning: Undefined array key 5 in C:\xampp\htdocs\wol\wol.php on line 41
  • magic packet Failed to send!

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;
    ?>
westmood
  • 9
  • 1
  • 2
    "Undefined array key 1 in C:\xampp\htdocs\wol\wol.php on line 41" so which one is line 41? – eis Jun 20 '21 at 07:05
  • `$addr_byte = explode(':', $this->mac);` - your MAC address should be `"38:D5:47:AA:69:A2"` not `"38-D5-47-AA-69-A2"` – msbit Jun 20 '21 at 07:20
  • Does this answer your question? [Wake on lan script that works](https://stackoverflow.com/questions/6055293/wake-on-lan-script-that-works) - the code is a little simpler, and handles the main error in the question (the Mac format) – AD7six Jun 20 '21 at 07:37
  • @msbit, yes, you are right! all the error messange is gone. but still can't send out the magic packet. – westmood Jun 20 '21 at 07:41
  • @AD7six yes, I can do this by the IT management tools so call Kaseya, but I want my colleague can do this by themselves via this website. – westmood Jun 20 '21 at 07:50
  • 1
    If you remove the uses of `@` in the code you'll get all the actual errors (which `@` suppresses). I'm not sure if the WOL implementation is quite right though; from https://en.wikipedia.org/wiki/Wake-on-LAN it should be sent to broadcast, not to a specific IP address. – msbit Jun 20 '21 at 07:51
  • Ok, you just need to fix the ip/host name then (as pointed out by several people now) :) – AD7six Jun 20 '21 at 07:52

1 Answers1

2

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.
eis
  • 51,991
  • 13
  • 150
  • 199
  • Thanks. I will learn more about this... now the error message only this sentence 'magic packet Failed to send!' left. – westmood Jun 20 '21 at 07:46