受欢迎的博客标签

远程开机方案 - Wake on Lan

Published

Wake on Lan

WoL 先决条件

需要PC 需要建立以太网连接。

最适用于始终插入以太网的台式机或笔记本电脑。

需要在计算机的 BIOS 中启用 WoL

Wake on Lan enabled

or
PCIE设备唤醒  enabled

需要在windows10中设置以太网控制器

在windows10设备驱动管理器中,找到 网络适配器 ,在第一个驱动 右键 -> 属性

允许计算机关闭此设备以节省电量
允许此设备唤醒计算机
只允许魔术包唤醒计算机

 

# Now enable Wake-on-LAN in your operating system
Allow this device to wake the computer  Enabled
Only allow a magic packet to wake the computer  Enabled
Wake On Magic Packet  Enabled

or

在 电源管理 中 勾选 允许此设备唤醒计算机 Enabled
在 高级 菜单中的属性找到 唤醒魔包 (Wake on Magic Packet) 设置为 启用 Enabled
在 电源管理 中 勾选 允许此设备唤醒计算机   Enabled

 

设置好 PC 后,设备准备好唤醒设备。

Magic Packet - Wake-on-LAN-enabled computers essentially wait for a "magic packet"

Wake-on-LAN-enabled computers essentially wait for a "magic packet" to arrive that includes the network card's MAC address in it. These magic packets are sent out by professional software made for any platform, but can also be sent by routers and internet-based websites. The typical ports used for WoL magic packets are UDP 7 and 9. Because your computer is actively listening for a packet, some power is feeding your network card which will result in your laptop's battery draining faster, so road warriors should take care to turn this off when you need to eke out some extra juice.

Magic Packet Format
A physical WakeOnLAN (Magic Packet) will look like this:

Synchronization Stream  Target MAC  Password (optional)
6                                        96                0, 4 or 6

Magic packet

Here is a screenshot of some WakeOnLAN traffic:

Send Magic Packet  in c#

Setting IP Address
Set the broadcast address for the Wake-on-LAN target PC within the network.

/// <summary>Broadcast Address</summary>
private const string IpAddress = "192.168.64.15";
Setting MAC Address
Set the MAC address for the Wake-on-LAN target NIC.

/// <summary>MAC Address</summary>
private const string MacAddress = "c8:60:00:dd:ea:b9";

 

 implementation of WoL in C# below

var macAddress = "01-00-00-00-00-02";                      // Our device MAC address
macAddress = Regex.Replace(macAddress, "[-|:]", "");       // Remove any semicolons or minus characters present in our MAC address
 
var sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
{
    EnableBroadcast = true
};
 
int payloadIndex = 0;
 
/* The magic packet is a broadcast frame containing anywhere within its payload 6 bytes of all 255 (FF FF FF FF FF FF in hexadecimal), followed by sixteen repetitions of the target computer's 48-bit MAC address, for a total of 102 bytes. */
byte[] payload = new byte[1024];    // Our packet that we will be broadcasting
 
// Add 6 bytes with value 255 (FF) in our payload
for (int i = 0; i < 6; i++)
{
    payload[payloadIndex] = 255;
    payloadIndex++;
}
 
// Repeat the device MAC address sixteen times
for (int j = 0; j < 16; j++)
{
    for (int k = 0; k < macAddress.Length; k += 2)
    {
        var s = macAddress.Substring(k, 2);
        payload[payloadIndex] = byte.Parse(s, NumberStyles.HexNumber);
        payloadIndex++;
    }
}
 
sock.SendTo(payload, new IPEndPoint(IPAddress.Parse("255.255.255.255"), 0));  // Broadcast our packet
sock.Close(10000);

c++

// C program to remotely Power On a PC over the
// internet using the Wake-on-LAN protocol.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/types.h>
 
int main()
{
    int i;
    unsigned char toSend[102],mac[6];
    struct sockaddr_in udpClient, udpServer;
    int broadcast = 1 ;
 
    // UDP Socket creation
    int udpSocket = socket(AF_INET, SOCK_DGRAM, 0);
 
    // Manipulating the Socket
    if (setsockopt(udpSocket, SOL_SOCKET, SO_BROADCAST,
                  &broadcast, sizeof broadcast) == -1)
    {
        perror("setsockopt (SO_BROADCAST)");
        exit(EXIT_FAILURE);
    }
    udpClient.sin_family = AF_INET;
    udpClient.sin_addr.s_addr = INADDR_ANY;
    udpClient.sin_port = 0;
 
    //Binding the socket
    bind(udpSocket, (struct sockaddr*)&udpClient, sizeof(udpClient));
 
    for (i=0; i<6; i++)
        toSend[i] = 0xFF;
 
    // Let the MAC Address be ab:cd:ef:gh:ij:kl
    mac[0] = 0xab;  // 1st octet of the MAC Address
    mac[1] = 0xcd;  // 2nd octet of the MAC Address
    mac[2] = 0xef;  // 3rd octet of the MAC Address
    mac[3] = 0xgh;  // 4th octet of the MAC Address
    mac[4] = 0xij;  // 5th octet of the MAC Address
    mac[5] = 0xkl;  // 6th octet of the MAC Address
 
    for (i=1; i<=16; i++)
        memcpy(&toSend[i*6], &mac, 6*sizeof(unsigned char));
 
    udpServer.sin_family = AF_INET;
 
    // Broadcast address
    udpServer.sin_addr.s_addr = inet_addr("10.89.255.255");
    udpServer.sin_port = htons(9);
 
    sendto(udpSocket, &toSend, sizeof(unsigned char) * 102, 0,
             (struct sockaddr*)&udpServer, sizeof(udpServer));
    return 0;
}

 

Wake On Lan over the Internet

Wake on Lan Magic Packets can be sent over the Internet

https://www.depicus.com/wake-on-lan/woli

 

 

useful links

sending Wake-on-LAN magic packets in C#

https://github.com/BornToBeRoot/NETworkManager