Use magic packet to start your computer

Source: Internet
Author: User

Many computers support wake-up from the network. Generally, these computers choose to enable the wake up function from the NIC Driver and choose magic packet to start.

For example, my atheros Nic supports:

After selecting this, record the MAC address of the NIC. You can use the getmac command or ipconfig.

Then, use the magic packet sender software to send packets on another computer in the same LAN to start the computer.

The packaging format of magic packet is very simple. Theoretically, magic packet can be packed in any network packet, But UDP or IPX is generally used.

The magic packet format is: First, it contains six ff, and then it is repeated for sixteen times to wake up the computer's Mac. For example, the packet should be like this:

FF 20 12 04 24 13 43 20 12 04 24 13 43 ......

Therefore, its encoding implementation is very simple. The following is a magic packet package program I wrote:

#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 -------

Note: If the wol function is enabled, the Mac and IP addresses of the NIC are in the active state. That is, the computer is active from the perspective of other machines on the LAN.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.