利用Magic Packet启动电脑

许多电脑支持从网络唤醒,这样的电脑一般从网卡驱动里选择启用Wake up功能,选择Magic Packet启动即可。

比如,我的Atheros网卡就是支持的:

        网卡驱动

选好这个之后,再记录下网卡的MAC地址,可以使用getmac命令,或者ipconfig。

        Magic Packet发包软件

然后在同局域网另一台电脑上使用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的。


PCnet Magic Packet(TM) Utility 1.00, September 9, 1997 ------------------------------------------------------- INTRODUCTION ~~~~~~~~~~~~ This is the release notice for the PCnet Magic Packet(TM) Utility Revision 1.00 software. All files are in DOS FAT format. AMD抯 Magic Packet utility is a convenient tool for system administrators for remotely powering up groups of workstations at preset times. The utility provides the following two basic functions: ?Creates a table of IP and hardware addresses for all hosts in a local area network. ?Associates each group of workstations/hosts to an alarm clock, and power on each group when the time comes. This utility supports the Microsoft Windows NT 4.0 and Windows 95 operating system environments. CONTENTS ~~~~~~~~~~~~~ The Rev 1.00 files are distributed as follows: Directory 1 - release.txt : the file you are viewing currently. - magpac.exe - magpac.hlp - conspawn.exe - magpac.cnt Directory 2 - msvcrt.dll - mfc42.dll - magpacbg.bmp Documents - mpuser.doc : Microsoft Word 6.0 document that describes the usage of the utility - testrept.doc : Test Report in Microsoft Word 6.0 format. DOCUMENTATION ~~~~~~~~~~~~~ This release contains version 1.0 of AMD's PCnet Magic Packet Utility User's Guide (MPuser.doc) and a test report (TestRept.Doc) describing the scope and content of tests that were conducted with this release of the utility. These documents are available in the distribution media. INSTALLATION INSTRUCTIONS ~~~~~~~~~~~~~~~~~~~~~~~~~ These instructions apply for both Windows 95 and Windows NT 4.0. 1. Open a DOS window 2. Create a directory such as c:\mputil 3. Copy all files contained in Directory 1 to the c:\mputil directory 4. For Windows 95, change to the windows system directory (default is c:\windows) directory For Windows NT, change to the Windows NT system32 sub-directory (default is c:\winnt\system32) Copy the *.dll files in Direc
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值