wake-on-lan

Wake-on-LAN简称WOL或WoL,我们一般将其称为“网络唤醒”、“远端唤醒”技术。
它的功能是让已经进入休眠状态或关机状态的电脑,透过局域网(多半为以太网)的另一端对其发令,使其从休眠状态唤醒、恢复成运作状态,或从关机状态转成开机状态。

该功能依赖软件和硬件。硬件就在于主板和网卡的支持,软件在于操作系统的电源管理器。

WOL协议

wol的是通过在局域网用udp口7或者9端口广播Magic Packet来实现的。

Magic Packet是一个广播帧(frame),在Magic Packet内,每次都会先有连续6个”FF”的前缀,即:FF FF FF FF FF FF,在连续6个”FF”后则开始带出MAC地址,有时还会带出4字节或6字节的密码,一旦经由网卡侦测、解读、研判(广播)Magic Packet的内容,内容中的MAC地址、密码若与电脑自身的地址、密码吻合,就会启动唤醒、开机的程序。

java实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
public class WakeOnLan {

public static final int PORT = 9;

public static void main(String[] args) {

if (args.length != 2) {
System.out.println("Usage: java WakeOnLan <broadcast-ip> <mac-address>");
System.out.println("Example: java WakeOnLan 192.168.0.255 00:0D:61:08:22:4A");
System.out.println("Example: java WakeOnLan 192.168.0.255 00-0D-61-08-22-4A");
System.exit(1);
}

String ipStr = args[0];
String macStr = args[1];

try {
byte[] macBytes = getMacBytes(macStr);
byte[] bytes = new byte[6 + 16 * macBytes.length];
for (int i = 0; i < 6; i++) {
bytes[i] = (byte) 0xff;
}
for (int i = 6; i < bytes.length; i += macBytes.length) {
System.arraycopy(macBytes, 0, bytes, i, macBytes.length);
}

InetAddress address = InetAddress.getByName(ipStr);
DatagramPacket packet = new DatagramPacket(bytes, bytes.length, address, PORT);
DatagramSocket socket = new DatagramSocket();
socket.send(packet);
socket.close();

System.out.println("Wake-on-LAN packet sent.");
}
catch (Exception e) {
System.out.println("Failed to send Wake-on-LAN packet: + e");
System.exit(1);
}

}

private static byte[] getMacBytes(String macStr) throws IllegalArgumentException {
byte[] bytes = new byte[6];
String[] hex = macStr.split("(\\:|\\-)");
if (hex.length != 6) {
throw new IllegalArgumentException("Invalid MAC address.");
}
try {
for (int i = 0; i < 6; i++) {
bytes[i] = (byte) Integer.parseInt(hex[i], 16);
}
}
catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid hex digit in MAC address.");
}
return bytes;
}


}

python的组件

python的awake组件已经实现这个功能,见https://pypi.org/project/awake/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Usage: awake [options] MAC1 [MAC2 MAC3 MAC...]

Options:
--version show program's version number and exit
-h, --help show this help message and exit
-p PORT, --port=PORT Destination port. (Default 9)
-b BROADCAST, --broadcast=BROADCAST
Broadcast ip of the network. (Default 255.255.255.255)
-d DESTINATION, --destination=DESTINATION
Destination ip/domain to connect and send the packet,
by default use broadcast.
-f FILE, --file=FILE Use a file with the list of macs, separated with -s,
by default \n. If any mac (line where -s \n), have the
"#" character, any following character is considered a
comment. Can be used multiple times for multiple
files.
-s SEPARATOR, --separator=SEPARATOR
Pattern to be use as a separator with the -f option.
(Default \n)
-q, --quiet Do not output informative messages.

注意事项

如果用python的awake工具,记得要加sudo,不然提示是

1
Sending magic packet to 255.255.255.255 with broadcast 255.255.255.255 MAC xx-xx-xx-xx-xx-xx port 9

但其实没有发送成功

参考


wake-on-lan
https://blog.fengcl.com/2018/09/11/wake-on-lan/
作者
frank
发布于
2018年9月11日
许可协议