WoL远程唤醒

Wake-on-LAN (WoL) is an Ethernet or Token Ring computer networking standard that allows a computer to be turned on or awakened by a network message.
该技术可以用来在远程打开局域网内的电脑。比如在办公室的内放置一个低功耗的树莓派(Raspberry Pi),并保持运行状态,利用MQTT协议在任何地方向树莓派发送开机指令,然后树莓派就会向办公室的局域网内发送wol广播消息,对应的电脑就会打开。
远程唤醒的关键在于几点:

  1. 内外网通信
  2. wol魔数包
  3. 硬件支持(BIOS设置、系统网卡设置)

内外网通信

好在只是传递简单的指令,因此完全可以用mqtt协议。有多家公司和机构都提供了免费的mqtt服务器,完全能够满足要求。如果需要隐私和防护,可以设计一个合适的json格式数据。

wol魔数包

wol基于UDP协议,端口号为7或9,发送固定格式的数据包,称为magic packet。packet由16进制数组成,包括都是ff的6个头字节和16次重复的6字节mac地址。
packet=0xFF6+mac16
例如

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
UDP Header:
|--source ip : 192.168.1.4
|--destination ip : 255.255.255.255
|--src port : 55656
|--dst port : 7
|-UDP length : 116
MAC Address:
00-E0-4C-31-03-AC
Password:
00 00 00 00 00 00
RAW Data:
FF FF FF FF FF FF 00 E0 4C 31 03 AC 00 E0 4C 31
03 AC 00 E0 4C 31 03 AC 00 E0 4C 31 03 AC 00 E0
4C 31 03 AC 00 E0 4C 31 03 AC 00 E0 4C 31 03 AC
00 E0 4C 31 03 AC 00 E0 4C 31 03 AC 00 E0 4C 31
03 AC 00 E0 4C 31 03 AC 00 E0 4C 31 03 AC 00 E0
4C 31 03 AC 00 E0 4C 31 03 AC 00 E0 4C 31 03 AC
00 E0 4C 31 03 AC 00 00 00 00 00 00

实际上,端口号不重要,可以使任意端口。根据协议规定,端口7为Echo Protocol,端口9为WoL。但是网卡只关心魔数包的内容,不管端口号是什么(电脑还没有开机,没有端口号的定义)

Wiki的解释:

Magic packet

The magic packet is a frame that is most often sent as a broadcast and that contains 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.

Since the magic packet is only scanned for the string above, and not actually parsed by a full protocol stack, it could be sent as payload of any network- and transport-layer protocol, although it is typically sent as a UDP datagram to port 0 (reserved port number),7 (Echo Protocol) or 9 (Discard Protocol),or directly over Ethernet as EtherType 0x0842. A connection-oriented transport-layer protocol like TCP is less suited for this task as it requires establishing an active connection before sending user data.

编程实现

1
2
3
4
5
6
7
8
9
10
# python 3.7
import socket
mac='00-E0-4C-31-03-AC'
sep=mac[2]
macaddress = mac.replace(sep, '')
packet = bytes.fromhex("ff" * 6 + macaddress * 16)
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.connect(("192.168.1.255", 9))
sock.send(packet)

socket只能发送字节流socket.send(bytes[, flags]),因此要将mac由str转换为bytes。

1
2
>>>bytes.fromhex('2Ef0 F1f2  ')
b'.\xf0\xf1\xf2'

bytes.fromhex()方法返回一个解码给定字符串的 bytes 对象。 字符串必须由表示每个字节的两个十六进制数码构成,其中的 ASCII 空白符会被忽略。

通过bytes.hex()将十六进制数转换为str打印,可以清楚看到packet的内容

1
2
>>> packet.hex()
'ffffffffffff00e04c3103ac00e04c3103ac00e04c3103ac00e04c3103ac00e04c3103ac00e04c3103ac00e04c3103ac00e04c3103ac00e04c3103ac00e04c3103ac00e04c3103ac00e04c3103ac00e04c3103ac00e04c3103ac00e04c3103ac00e04c3103ac'

用wireshark监听的结果:

可以看到packet包内的数据和接收到的数据是一致的。

经测试,任意的端口号都可以用。

扩展:广播地址

广播地址必须使用UDP协议,工作在局域网中。

255.255.255.255 只能在所在子网中传播,不能被路由器向外发送

192.168.1.255 可以被路由器发送到其他的子网,也可以作为本子网的广播

经测试,在局域网中DHCP协议可以使用255.255.255.255,而用255.255.255.255的WOL协议则被路由器丢弃了,只有用192.168.1.255地址广播才会被路由器转发。因此,未来兼容新,最好使用类似192.168.1.255的地址,不用255.255.255.255。

常用端口列表

Port TCP UDP SCTP DCCP Description
0 Reserved Reserved
N/A In programming APIs (not in communication between hosts), requests a system-allocated (dynamic) port[6]
1 Yes Assigned TCP Port Service Multiplexer (TCPMUX). Historic. Both TCP and UDP have been assigned to TCPMUX by IANA,[2] but by design only TCP is specified.[7]
5 Assigned Assigned Remote Job Entry[8] was historically using socket 5 in its old socket form, while MIB PIM has identified it as TCP/5[9] and IANA has assigned both TCP and UDP 5 to it.
7 Yes Yes Echo Protocol[10][11]
9 Yes Yes Yes[12] Discard Protocol[13]
No Unofficial Wake-on-LAN[14]
11 Yes Yes Active Users (systat) service)[15][16]
13 Yes Yes Daytime Protocol[17]
15 Unofficial No Previously netstat service[2][15]
17 Yes Yes Quote of the Day (QOTD)[18]
18 Yes Yes Message Send Protocol[19][20]
19 Yes Yes Character Generator Protocol (CHARGEN)[21]
20 Yes Assigned Yes[12] File Transfer Protocol (FTP) data transfer[11]
21 Yes Assigned Yes[12] File Transfer Protocol (FTP) control (command)[11][12][22][23]
22 Yes Assigned Yes[12] Secure Shell (SSH),[11] secure logins, file transfers (scp, sftp) and port forwarding
23 Yes Assigned Telnet protocol—unencrypted text communications[11][24]
25 Yes Assigned Simple Mail Transfer Protocol (SMTP),[11][25] used for email routing between mail servers
28 Unofficial Palo Alto Networks’ Panorama High Availability (HA) sync encrypted port.[26]
37 Yes Yes Time Protocol[27]
42 Assigned Yes Host Name Server Protocol[28]
43 Yes Assigned WHOIS protocol[29][30][31]
47 Reserved Reserved
49 Yes Yes TACACS Login Host protocol.[32] TACACS+, still in draft which is an improved but distinct version of TACACS, only uses TCP 49.[33]
51 Reserved Reserved Historically used for Interface Message Processor logical address management,[34] entry has been removed by IANA on 2013-05-25
52 Assigned Assigned Xerox Network Systems (XNS) Time Protocol. Despite this port being assigned by IANA, the service is meant to work on SPP (ancestor of IPX/SPX), instead of TCP/IP.[35]
53 Yes Yes Domain Name System (DNS)[36][11]
54 Assigned Assigned Xerox Network Systems (XNS) Clearinghouse (Name Server). Despite this port being assigned by IANA, the service is meant to work on SPP (ancestor of IPX/SPX), instead of TCP/IP.[35]
56 Assigned Assigned Xerox Network Systems (XNS) Authentication Protocol. Despite this port being assigned by IANA, the service is meant to work on SPP (ancestor of IPX/SPX), instead of TCP/IP.[35]
58 Assigned Assigned Xerox Network Systems (XNS) Mail. Despite this port being assigned by IANA, the service is meant to work on SPP (ancestor of IPX/SPX), instead of TCP/IP.[35]
61 Reserved Reserved Historically assigned to the NIFTP-Based Mail protocol,[37] but was never documented in the related IEN.[38] The port number entry was removed from IANA’s registry on 2017-05-18.[2]
67 Assigned Yes Bootstrap Protocol (BOOTP) server;[11] also used by Dynamic Host Configuration Protocol (DHCP)
68 Assigned Yes Bootstrap Protocol (BOOTP) client;[11] also used by Dynamic Host Configuration Protocol (DHCP)
69 Assigned Yes Trivial File Transfer Protocol (TFTP)[11][39][40][41]
70 Yes Assigned Gopher) protocol[42]
71–74 Yes Yes NETRJS protocol[43][44][45]
79 Yes Assigned Finger protocol[11][46][47]
80 Yes Yes Yes[12] Hypertext Transfer Protocol (HTTP)[48][49] uses TCP in versions 1.x and 2. HTTP/3 uses QUIC,[50] a transport protocol on top of UDP.
81 Unofficial TorPark onion routing[verification needed]
82 Unofficial TorPark control[verification needed]
83 Yes Assigned MIT ML Device, networking file system[51]
88 Yes Yes Kerberos)[11][52][53] authentication system
90 Unofficial Unofficial PointCast (dotcom))[2][third-party source needed]
95 Yes Assigned SUPDUP, terminal-independent remote login[54]
101 Yes Assigned NIC host name[55]
102 Yes Assigned ISO Transport Service Access Point (TSAP) Class 0 protocol;[56][57]
104 Yes Yes Digital Imaging and Communications in Medicine (DICOM; also port 11112)
105 Yes Yes CCSO Nameserver[58]
107 Yes Yes Remote User Telnet Service (RTelnet)[59]
108 Yes Yes IBM Systems Network Architecture (SNA) gateway access server
109 Yes Assigned Post Office Protocol, version 2 (POP2)[60]
110 Yes Assigned Post Office Protocol, version 3 (POP3)[11][61][62]
111 Yes Yes Open Network Computing Remote Procedure Call (ONC RPC, sometimes referred to as Sun RPC)
113 Yes No Ident, authentication service/identification protocol,[11][63] used by IRC servers to identify users
Yes Assigned Authentication Service (auth), the predecessor to identification protocol. Used to determine a user’s identity of a particular TCP connection.[64]
115 Yes Assigned Simple File Transfer Protocol[11][65]
117 Yes Yes UUCP Mapping Project (path service)[citation needed]
118 Yes Yes Structured Query Language (SQL) Services[jargon]
119 Yes Assigned Network News Transfer Protocol (NNTP),[11] retrieval of newsgroup messages[66][67]
123 Assigned Yes Network Time Protocol (NTP), used for time synchronization[11]
126 Yes Yes Formerly Unisys Unitary Login, renamed by Unisys to NXEdit. Used by Unisys Programmer’s Workbench for Clearpath MCP, an IDE for Unisys MCP software development
135 Yes Yes DCE endpoint resolution
Yes Yes Microsoft EPMAP (End Point Mapper), also known as DCE/RPC Locator service,[68] used to remotely manage services including DHCP server, DNS server and WINS. Also used by DCOM
137 Yes Yes NetBIOS Name Service, used for name registration and resolution)[69][70]
138 Assigned Yes NetBIOS Datagram Service[11][69][70]
139 Yes Assigned NetBIOS Session Service[69][70]
143 Yes Assigned Internet Message Access Protocol (IMAP),[11] management of electronic mail messages on a server[71]
152 Yes Yes Background File Transfer Program (BFTP)[72][importance?]
153 Yes Yes Simple Gateway Monitoring Protocol (SGMP), a protocol for remote inspection and alteration of gateway management information[73]
156 Yes Yes Structured Query Language (SQL) Service[jargon]
158 Yes Yes Distributed Mail System Protocol (DMSP, sometimes referred to as Pcmail)[74][importance?]
161 Assigned Yes Simple Network Management Protocol (SNMP)[75][citation needed][11]
162 Yes Yes Simple Network Management Protocol Trap (SNMPTRAP)[75][76][citation needed]
170 Yes Yes Network PostScript print server
177 Yes Yes X Display Manager Control Protocol (XDMCP), used for remote logins to an X Display Manager) server[77][self-published source]
179 Yes Assigned Yes[12] Border Gateway Protocol (BGP),[78] used to exchange routing and reachability information among autonomous systems) (AS) on the Internet
194 Yes Yes Internet Relay Chat (IRC)[79]
199 Yes Yes SNMP Unix Multiplexer (SMUX)[80]
201 Yes Yes AppleTalk Routing Maintenance
209 Yes Assigned Quick Mail Transfer Protocol[81][self-published source]
210 Yes Yes ANSI Z39.50
213 Yes Yes Internetwork Packet Exchange (IPX)
218 Yes Yes Message posting protocol (MPP)
220 Yes Yes Internet Message Access Protocol (IMAP), version 3
225–241 Reserved Reserved
249–255 Reserved Reserved
259 Yes Yes Efficient Short Remote Operations (ESRO)
262 Yes Yes Arcisdms
264 Yes Yes Border Gateway Multicast Protocol (BGMP)
280 Yes Yes http-mgmt
300 Unofficial ThinLinc Web Access
308 Yes Novastor Online Backup
311 Yes Assigned macOS Server Admin[11] (officially AppleShare IP Web administration[2])
318 Yes Yes PKIX Time Stamp Protocol (TSP)
319 Yes Precision Time Protocol (PTP) event messages
320 Yes Precision Time Protocol (PTP) general messages
350 Yes Yes Mapping of Airline Traffic over Internet Protocol (MATIP) type A
351 Yes Yes MATIP type B
356 Yes Yes cloanto-net-1 (used by Cloanto Amiga Explorer and VMs)
366 Yes Yes On-Demand Mail Relay (ODMR)
369 Yes Yes Rpc2portmap
370 Yes Yes codaauth2, Coda authentication server
Yes securecast1, outgoing packets to NAI‘s SecureCast servers[82]As of 2000
371 Yes Yes ClearCase albd
376 Yes Yes Amiga Envoy Network Inquiry Protocol
383 Yes Yes HP data alarm manager
384 Yes Yes A Remote Network Server System
387 Yes Yes AURP (AppleTalk Update-based Routing Protocol)[83]
388 Yes Assigned Unidata LDM near real-time data distribution protocol[84][self-published source][85][self-published source]
389 Yes Assigned Lightweight Directory Access Protocol (LDAP)[11]
399 Yes Yes Digital Equipment Corporation DECnet+ (Phase V) over TCP/IP (RFC1859)
401 Yes Yes Uninterruptible power supply (UPS)
427 Yes Yes Service Location Protocol (SLP)[11]
433 Yes Yes NNTP, part of Network News Transfer Protocol
434 Yes Yes Mobile IP Agent (RFC 5944)
443 Yes Yes Yes[12] Hypertext Transfer Protocol Secure (HTTPS)[48][49] uses TCP in versions 1.x and 2. HTTP/3 uses QUIC,[50] a transport protocol on top of UDP.
444 Yes Yes Simple Network Paging Protocol (SNPP), RFC 1568
445 Yes Yes Microsoft-DS (Directory Services) Active Directory,[86] Windows shares
Yes Assigned Microsoft-DS (Directory Services) SMB[11] file sharing
464 Yes Yes Kerberos) Change/Set password
465 Yes No URL Rendezvous Directory for SSM (Cisco protocol)[importance?]
Yes No Authenticated SMTP[11] over TLS/SSL (SMTPS)[87]
475 Yes Yes tcpnethaspsrv, Aladdin Knowledge Systems Hasp services
476–490 Unofficial Unofficial Centro Software ERP ports
491 Unofficial GO-Global remote access and application publishing software
497 Yes Yes Retrospect)
500 Assigned Yes Internet Security Association and Key Management Protocol (ISAKMP) / Internet Key Exchange (IKE)[11]
502 Yes Yes Modbus Protocol
504 Yes Yes Citadel, multiservice protocol for dedicated clients for the Citadel groupware system
510 Yes Yes FirstClass Protocol (FCP), used by FirstClass client/server groupware system
512 Yes Rexec, Remote Process Execution
Yes comsat, together with biff)
513 Yes rlogin
Yes Who[88]
514 Unofficial Remote Shell, used to execute non-interactive commands on a remote system (Remote Shell, rsh, remsh)
No Yes Syslog,[11] used for system logging
515 Yes Assigned Line Printer Daemon (LPD),[11] print service
517 Yes Talk
518 Yes NTalk
520 Yes efs, extended file name server
Yes Routing Information Protocol (RIP)
521 Yes Routing Information Protocol Next Generation (RIPng)
524 Yes Yes NetWare Core Protocol (NCP) is used for a variety things such as access to primary NetWare server resources, Time Synchronization, etc.
525 Yes Timed, Timeserver
530 Yes Yes Remote procedure call (RPC)
532 Yes Assigned netnews[11]
533 Yes netwall, For Emergency Broadcasts
540 Yes Unix-to-Unix Copy Protocol (UUCP)
542 Yes Yes commerce (Commerce Applications)
543 Yes klogin, Kerberos) login
544 Yes kshell, Kerberos) Remote shell
546 Yes Yes DHCPv6 client
547 Yes Yes DHCPv6 server
548 Yes Assigned Apple Filing Protocol (AFP) over TCP[11]
550 Yes Yes new-rwho, new-who[88]
554 Yes Yes Real Time Streaming Protocol (RTSP)[11]
556 Yes Remotefs, RFS, rfs_server
560 Yes rmonitor, Remote Monitor
561 Yes monitor
563 Yes Yes NNTP over TLS/SSL (NNTPS)
564 Unofficial 9P) (Plan 9)
585 No No Previously assigned for use of Internet Message Access Protocol over TLS/SSL (IMAPS), now deregisterd in favour of port 993.[89]
587 Yes Assigned email message submission[11][90] (SMTP)
591 Yes FileMaker 6.0 (and later) Web Sharing (HTTP Alternate, also see port 80)
593 Yes Yes HTTP RPC Ep Map, Remote procedure call over Hypertext Transfer Protocol, often used by Distributed Component Object Model services and Microsoft Exchange Server
601 Yes Reliable Syslog Service — used for system logging
604 Yes TUNNEL profile,[91] a protocol for BEEP peers to form an application layer tunnel
623 Yes ASF Remote Management and Control Protocol (ASF-RMCP) & IPMI Remote Management Protocol
625 Unofficial No Open Directory Proxy (ODProxy)[11]
631 Yes Yes Internet Printing Protocol (IPP)[11]
Unofficial Unofficial Common Unix Printing System (CUPS) administration console (extension to IPP)
635 Yes Yes RLZ DBase
636 Yes Assigned Lightweight Directory Access Protocol over TLS/SSL (LDAPS)[11]
639 Yes Yes Multicast Source Discovery Protocol, MSDP
641 Yes Yes SupportSoft Nexus Remote Command (control/listening), a proxy gateway connecting remote control traffic
643 Yes Yes SANity
646 Yes Yes Label Distribution Protocol (LDP), a routing protocol used in MPLS networks
647 Yes DHCP Failover protocol[92]
648 Yes Registry Registrar Protocol (RRP)[93]
651 Yes Yes IEEE-MMS
653 Yes Yes SupportSoft Nexus Remote Command (data), a proxy gateway connecting remote control traffic
654 Yes Media Management System (MMS) Media Management Protocol (MMP)[94]
655 Yes Yes Tinc) VPN daemon
657 Yes Yes IBM RMC (Remote monitoring and Control) protocol, used by System p5 AIX Integrated Virtualization Manager (IVM)[95] and Hardware Management Console to connect managed logical partitions (LPAR) to enable dynamic partition reconfiguration
660 Yes Assigned macOS Server administration,[2] version 10.4 and earlier[11]
666 Yes Yes Doom), first online first-person shooter
Unofficial airserv-ng, aircrack-ng‘s server for remote-controlling wireless devices
674 Yes Application Configuration Access Protocol (ACAP)
688 Yes Yes REALM-RUSD (ApplianceWare Server Appliance Management Protocol)
690 Yes Yes Velneo Application Transfer Protocol (VATP)
691 Yes MS Exchange Routing
694 Yes Yes Linux-HA high-availability heartbeat
695 Yes IEEE Media Management System over SSL (IEEE-MMS-SSL)[96]
698 Yes Optimized Link State Routing (OLSR)
700 Yes Extensible Provisioning Protocol (EPP), a protocol for communication between domain name registries and registrars (RFC 5734)
701 Yes Link Management Protocol (LMP),[97] a protocol that runs between a pair of nodes) and is used to manage traffic engineering (TE) links
702 Yes IRIS[98][99] (Internet Registry Information Service) over BEEP (Blocks Extensible Exchange Protocol)[100] (RFC 3983)
706 Yes Secure Internet Live Conferencing) (SILC)
711 Yes Cisco Tag Distribution Protocol[101][102][103]—being replaced by the MPLS Label Distribution Protocol[104]
712 Yes Topology Broadcast based on Reverse-Path Forwarding routing protocol (TBRPF; RFC 3684)
749 Yes Yes Kerberos (protocol)) administration[11]
750 Yes kerberos-iv, Kerberos) version IV
751 Unofficial Unofficial kerberosmaster, [Kerberos](https://en.wikipedia.org/wiki/Kerberos(protocol)) authentication
752 Unofficial passwdserver, [Kerberos](https://en.wikipedia.org/wiki/Kerberos(protocol)) password (kpasswd) server
753 Yes Yes Reverse Routing Header (RRH)[105]
Unofficial userregserver, [Kerberos](https://en.wikipedia.org/wiki/Kerberos(protocol)) userreg server
754 Yes Yes tell send
Unofficial krb5prop, [Kerberos](https://en.wikipedia.org/wiki/Kerberos(protocol)) v5 slave propagation
760 Unofficial Unofficial krbupdate [kreg], Kerberos) registration
782 Unofficial Conserver serial-console management server
783 Unofficial SpamAssassin spamd daemon
800 Yes Yes mdbs-daemon
802 Yes Yes MODBUS/TCP Security[106]
808 Unofficial Microsoft Net.TCP Port Sharing Service
829 Yes Assigned Certificate Management Protocol[107]
830 Yes Yes NETCONF over SSH
831 Yes Yes NETCONF over BEEP
832 Yes Yes NETCONF for SOAP over HTTPS
833 Yes Yes NETCONF for SOAP over BEEP
843 Unofficial Adobe Flash[108]
847 Yes DHCP Failover protocol
848 Yes Yes Group Domain Of Interpretation (GDOI) protocol
853 Yes Yes DNS over TLS (RFC 7858)
860 Yes iSCSI (RFC 3720)
861 Yes Yes OWAMP control (RFC 4656)
862 Yes Yes TWAMP control (RFC 5357)
873 Yes rsync file synchronization protocol
888 Unofficial cddbp, CD DataBase (CDDB) protocol (CDDBP)
Unofficial IBM Endpoint Manager Remote Control
897 Unofficial Unofficial Brocade SMI-S RPC
898 Unofficial Unofficial Brocade SMI-S RPC SSL
902 Unofficial Unofficial VMware ESXi[109][110]
903 Unofficial VMware ESXi[109][110]
953 Yes Reserved BIND remote name daemon control (RNDC)[111][112]
981 Unofficial Remote HTTPS management for firewall devices running embedded Check Point VPN-1 software[113]
987 Unofficial Microsoft Remote Web Workplace, a feature of Windows Small Business Server[114]
989 Yes Yes FTPS Protocol (data), FTP over TLS/SSL
990 Yes Yes FTPS Protocol (control), FTP over TLS/SSL
991 Yes Yes Netnews Administration System (NAS)[115]
992 Yes Yes Telnet protocol over TLS/SSL
993 Yes Assigned Internet Message Access Protocol over TLS/SSL (IMAPS)[11]
994 Reserved Reserved Previously assigned to Internet Relay Chat over TLS/SSL (IRCS), but was not used in common practice.
995 Yes Yes Post Office Protocol 3 over TLS/SSL (POP3S)[11]
1010 Unofficial ThinLinc web-based administration interface[116]
1011–1020 Reserved Reserved
1023 Reserved Reserved [2]
Unofficial Unofficial z/OS Network File System (NFS) (potentially ports 991–1023)[79][80][117]
虽然很不要脸,但是还请您多多打赏 ^_^