1、小米路由器AX3600设置部分

a,设置arp绑定,vi编辑/etc/ethers

vi /etc/ethers

192.168.0.XX 30:9c:23:6d:12:XX

检查设置: cat /etc/ethers

b,设置开机自动添加静态arp, 个人是在rc.local里添加,重启后通过arp -a可以看到成功绑定。

vi /etc/rc.local

添加

ip neigh add 192.168.0.X lladdr 30:9c:23:6d:12:XX nud permanent dev br-lan
exit 0

c,个人在小米后台设置了DHCP静态IP分配,固定了需要wol的电脑ip。并转发了端口,(个人用了3389跟远程共用一个端口) 路由器部分设置完成。

2、外网设置部分

这部分参考网上文章采用了php来发送唤醒包

wol.class.php 和 wol.php 文件。网上可以找到代码,也可以采用其他方式来发送唤醒包。

wol.php调用代码
<?php
include("wol.class.php");
 
$WOL = new WOL("你的IP或动态域名","目标计算机网卡mac码","唤醒端口");
$status = $WOL->wake_on_wan();
 
echo $status;
?>
 wol.class.php主代码
<?php
/**
* 实现网络唤醒功能
*/
class WOL
{
    private $hostname;    // 唤醒设备的url地址
    private $mac;         // 唤醒设备的mac地址
    private $port;        // 唤醒设备的端口
    private $ip;          // 唤醒设备的ip地址(不是必须的,程序会自动根据$hostname来获取对应的ip)
 
    private $msg = array(
        0 => "目标机器已经是唤醒的.",
        1 => "socket_create 方法执行失败",
        2 => "socket_set_option 方法执行失败",
        3 => "magic packet 发送成功!",
        4 => "magic packet 发送成功!"
    );
     
    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]; // 如果设备已经是唤醒的就不做其它操作了
        }
        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;
            // 通过 UDP 发送数据包
            $s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
             
            if ($s == false)
            {
                return $this->msg[1]; // socket_create 执行失败
            }
 
            $set_opt = @socket_set_option($s, 1, 6, TRUE);
 
            if ($set_opt < 0)
            {
                return $this->msg[2]; // socket_set_option 执行失败
            }
 
            $sendto = @socket_sendto($s, $msg, strlen($msg), 0, $this->ip, $this->port);
             
            if ($sendto)
            {
                socket_close($s);
                return $this->msg[3]; // magic packet 发送成功!
            }
 
            return $this->msg[4]; // magic packet 发送失败!
             
        }
    }
 
    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);
    }
 
}
?>