许多电脑支持从网络唤醒,这样的电脑一般从网卡驱动里选择启用Wake up功能,选择Magic Packet启动即可。
比如,我的Atheros网卡就是支持的:
选好这个之后,再记录下网卡的MAC地址,可以使用getmac命令,或者ipconfig。
然后在同局域网另一台电脑上使用Magic Packet Sender软件发包,即可启动电脑。
Magic Packet的包格式很简单,理论上可以在任意网络封包中打包Magic Packet,不过一般选择UDP或IPX。
Magic Packet的格式是,首先包含六个FF,然后是重复十六次待唤醒电脑的MAC。比如上图的封包,就应该是这样:
FF FF FF FF FF FF 20 12 04 24 13 43 20 12 04 24 13 43 ......
所以,它的编码实现是很简单的,下面附上我自己写的Magic Packet发包程序:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
void fill_magic_buf(void *magic_buf, void *mac)
{
int i;
char *ptr;
ptr = magic_buf;
memset(ptr, 0xFF, 6);
ptr += 6;
for(i = 0; i < 16; ++i) {
memcpy(ptr, mac, 6);
ptr += 6;
}
}
void usage(void)
{
printf("usage...\n");
}
int main(int argc, char **argv)
{
int s;
int packet_num = 10;
char c;
unsigned char mac[6] = {0x00, 0x1A, 0x92, 0xE5, 0x1B, 0xA7};
char dstip[256] = "192.168.9.180";
int port = 9;
struct sockaddr_in address;
char magic_buf[6 + 6 * 16] = {0};
daemon(0,0); /* run in background */
while((c = getopt(argc, argv, "d:m:p:")) != -1) {
switch(c) {
case 'd':
strcpy(dstip, optarg);
break;
case 'm':
sscanf(optarg, "%x:%x:%x:%x:%x:%x",
(unsigned int*)&mac[0], (unsigned int*)&mac[1],
(unsigned int*)&mac[2], (unsigned int*)&mac[3],
(unsigned int*)&mac[4], (unsigned int*)&mac[5]);
break;
case 'p':
port = atoi(optarg);
break;
default:
usage();
return -1;
}
}
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s == -1) {
perror("Opening socket");
exit(EXIT_FAILURE);
}
memset(&address, 0, sizeof(struct sockaddr_in));
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr(dstip);
address.sin_port = htons(port);
fill_magic_buf(magic_buf, mac);
/* ten packets. TODO: use ping to check whether the destination is on or else. */
while (packet_num-- > 0) {
if (sendto(s, magic_buf, sizeof(magic_buf), 0,
(struct sockaddr *)&address, sizeof(address)) < 0) {
printf("sendto\n");
exit(EXIT_FAILURE);
}
sleep(1);
}
exit(EXIT_SUCCESS);
}
------12-12-3-------
注意:处于关机状态的网卡,如果启用了WOL功能,此时网卡的MAC和IP都是处于维护(Active)状态的。即从局域网上其它机器的角度,这台电脑是Active的。