Magic Packet

Magic Packet白皮书介绍:

  The basic technical details of Magic Packet Technologyare simple and easy to understand. There is also a sec-ond set of details, which will be implementation spe-cific. In other words, silicon- or gate-levelimplementations of Magic Packet Technology may dif-fer from AMD's approach and be completely interoper-able, as long as the basic feature set is maintained.

https://wenku.baidu.com/view/d5a3282e453610661ed9f487.html

 AMD公司出的网络唤醒协议,即发送远程唤醒数据包,Magic Packet虽然只是AMD公司所开发的一项技术,但是受到了几乎所有网卡制造商的支持,因此,大多数流行网卡都能与之很好地兼容。(2010年后生产的主板)

WOL原理

WOL(Wake on Lan),即局域网唤醒,从根本上来说是硬件设备提供的一项管理功能,该功能可以当电脑处于关机或休眠状态时,通过给网卡发送特定的数据包从而命令网卡向主板发送开机指令,进而实现上电开机.可见,网络唤醒需要硬件(主要是主板和网卡)的支持.

硬件设置

进入BIOS,将“Power Management Setup”中的“Wake Up On LAN”或“Resume by LAN”项设置为“Enable”或“On”,类似于这样,因为主板不一样,BIOS设置位置有可能有差异

比如我的主板设置是在:Setting-Advanced-Wake up event setup - Resume By PCI-E Device 设置为Enable.

有的人说还要设置boot的第一启动项为network,我试过不需要滴!

软件设置

BIOS设置好了,硬件已经满足条件了,然后需要OS级别的软件设置

Windows设置:

设备管理器-网卡设备-属性-高级, 在列表里能找到“Wakeup Capabilities(唤醒功能)”设置值为“MagicPacket”或”Both”.

Linux设置:

先通过 ifconfig 查看要wol的网卡.

这里需要说下,好多网友说没有eth0, 对! 我也没有这个网卡的信息,因为你用的肯定是Ubuntu的衍生版,在Ubuntu 16.04之后以太网卡名称由eth0,变成了enp3s0,还有systemd替换掉了initd来引导系统,参考:   Linux网卡命名enp3s0说明

用ethtool命令 打印网卡信息

sudo ethtool enp3s0

打印结果:

....
....
Supports Wake-on: pumbg
Wake-on: g
Current message level: 0x00000033 ()
drv probe ifdown ifup

Wake-on 参数:

d 表示禁用disable

g表示启用great

如果wake-on参数为d就要启用wol, 启用命令:

sudo ethtool -s enp3s0 wol g

必须要用sudo 管理员权限否则会提示:  Cannot get current wake-on-lan settings: Operation not permitted

-s 参数是修改以太网设备设置

supports wake-on 中的参数

p Wake on phy activity
u Wake on unicast messages
m Wake on multicast messages
b Wake on broadcast messages
a Wake on AR

获取MAC地址

Windows 直接在网络设备-属性里面, linux使用ifconfig 命令查看

例如linux:

enp3s0: flags=<UP,BROADCAST,RUNNING,MULTICAST>  mtu
inet 192.168.1.213 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::a680:65a4:a006:5bbc prefixlen scopeid 0x20<link>
ether d8:cb:8a:3f::d2 txqueuelen (以太网)
RX packets bytes (1.0 GB)
RX errors dropped overruns frame
TX packets bytes (73.3 MB)
TX errors dropped overruns carrier collisions

网络唤醒

到这;已经有了BIOS的支持,有了OS wake-on的支持,又知道了MAC地址,说明被控制(唤醒)机已经Ready了.

局域网唤醒

现在我们需要局域网的电脑或者设备去访问,访问不是随随便便访问的,因为Magic Packet是一种协议规则,必须要用规则发送,关于规则可看下一个段落解析Magic Packet包

我们用GUI工具去访问,常用的WOL工具有

WakeOnLanGui

下载地址:https://www.depicus.com/wake-on-lan/wake-on-lan-gui

WakeMeOnLan

下载地址:http://wakemeonlan.findmysoft.com

参数IP地址,MAC地址,任意一个端口号,IP地址也可以直接填写255.255.255.255

Linux下我没有找到GUI工具,只有wakeonlan命令了

sudo apt install wakeonlan

唤醒目标主机:

wakeonlan d8:cb:8a:3f::d2

输出:

Sending magic packet to 255.255.255.255: with d8:cb:8a:3f:32:d2

局域网手机唤醒

在同一个局域网下,手机也可以唤醒目标主机

Android APP:

Wake On Lan

下载地址:https://dl.iplaysoft.com/files/3869.html

iphone APP:

RemoteBoot

      

下载地址:appstore自己搜.....

 准备放大招了!!!!! 

互联网远程唤醒

虽然 WOL (Wake on Lan) 网络唤醒原本的设计就是 LAN 局域网环境下使用的,但其实我们也是可以想办法让其在 WAN 广域网 (即互联网) 下使用——Wake On Wan。这样,在公司唤醒家里的电脑(在家唤醒公司电脑...........)

第一步:设置端口映射(虚拟服务器)

因为广域网不知道我们局域网内对应的主机是哪一个,所以只访问外网的IP不会把Magic Packet包发到局域网指定的电脑上.我们需要路由器端口映射的支持

登陆路由器找到 传输控制-NAT设置-虚拟服务器

路由器必须为顶级路由而非二级即二级以下

如果路由器LAN设置的是DHCP动态分配IP,有可能这次分配的局域网IP与重启之后局域网IP不统一(如果设备少于DHCP分配区间,IP会一直续租), 所以最好做一个静态地址分配或者IP与MAC绑定.

对于外网,如果IP是运营商固定IP可以直接使用公网IP,如果非固定IP(自动获取IP与PPPoE拨号)可以通过花生壳进行DDNS动态域名解析.使用动态域名替代公网IP.

第二步:查看公网IP

通过站长工具 http://ip.chinaz.com/  查看对应公网IP

第三步:网络访问目标

OK! 此时,可以通过手机APP进行互联网的唤醒了,关闭WiFi,打开4G.好吧,不关WiFi也行.

当我们按BOOT时,就会通过公网IP端口映射到指定的主机,发送Magic Packet包唤醒电脑.

depicus 提供了在线远程唤醒   (现在貌似唤不起来了!!!)

https://www.depicus.com/wake-on-lan/woli

网站这样解释:

Wake on Lan Magic Packets can be sent over the Internet - why not try waking up one of your machines with our free Wake On Wan Service. Want a quick way to use this page ?

Bookmark https://www.depicus.com/wake-on-lan/woli?m=001143BDA600&i=82.110.108.30&s=255.255.255.255&p=4321 will get you straight there without the need to press those pesky send buttons.

换句话说,我们可以不用下载APP,只要保存一个书签,当需要的时候访问书签即可.

https://www.depicus.com/wake-on-lan/woli?m=d8cb8a3f32d2&i=58.37.39.82&s=255.255.255.255&p=4321 

TeamViewer 自带Lan网络唤醒

在TeamViewer-其他-常规-网络设置中就有自带的Lan网络唤醒

填写公共地址IP或端口映射对应的NAT网址,手机端TeamViewer登陆对应的账户,当主机关机时我们就可以通过手机端TeamViewer唤醒电脑

解析Magic Packet包

我们已经知道,Magic Packet是一种协议规则,必须遵从规则发送,Magic Packet的包格式很简单,首先是六个FF,然后是重复十六次待唤醒电脑的MAC

FF FF FF FF FF FF d8 cb 8a 3f  d2 d8 cb 8a 3f  d2 ....... 

理论上可以在任意网络封包中打包Magic Packet,不过一般选择UDP或IPX

Python代码:

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
Small module for use with the wake on lan protocol. """
from __future__ import absolute_import
from __future__ import unicode_literals import argparse
import socket
import struct
import re BROADCAST_IP = '255.255.255.255'
DEFAULT_PORT = 9 def create_magic_packet(macaddress):
"""
Create a magic packet. A magic packet is a packet that can be used with the for wake on lan
protocol to wake up a computer. The packet is constructed from the
mac address given as a parameter. Args:
macaddress (str): the mac address that should be parsed into a
magic packet. """
if len(macaddress) == 12:
pass
elif len(macaddress) == 17:
sep = macaddress[2]
macaddress = macaddress.replace(sep, '')
else:
raise ValueError('Incorrect MAC address format') # Pad the synchronization stream
data = b'FFFFFFFFFFFF' + (macaddress * 16).encode()
send_data = b'' # Split up the hex values in pack
for i in range(0, len(data), 2):
send_data += struct.pack(b'B', int(data[i: i + 2], 16))
return send_data def send_magic_packet(*macs, **kwargs):
"""
Wake up computers having any of the given mac addresses. Wake on lan must be enabled on the host device. Args:
macs (str): One or more macaddresses of machines to wake. Keyword Args:
ip_address (str): the ip address of the host to send the magic packet
to (default "255.255.255.255")
port (int): the port of the host to send the magic packet to
(default 9) """
packets = []
ip = kwargs.pop('ip_address', BROADCAST_IP) port = kwargs.pop('port', DEFAULT_PORT)
for k in kwargs:
raise TypeError('send_magic_packet() got an unexpected keyword '
'argument {!r}'.format(k)) for mac in macs:
packet = create_magic_packet(mac)
packets.append(packet) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) # 如果输入的是域名,将域名转换为IP
ipv4_regex = re.compile(r'(?:25[0-5]|2[0-4]\d|[0-1]?\d?\d)(?:\.(?:25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}',re.IGNORECASE)
if not ipv4_regex.match(ip):
ip=socket.gethostbyname(ip)
sock.connect((ip, port))
for packet in packets:
sock.send(packet)
sock.close()
print('sent to '+ip) def main(argv=None):
"""
Run wake on lan as a CLI application. """
parser = argparse.ArgumentParser(
description='Wake one or more computers using the wake on lan'
' protocol.')
parser.add_argument(
'macs',
metavar='mac address',
nargs='+',
help='The mac addresses or of the computers you are trying to wake.')
parser.add_argument(
'-i',
metavar='ip',
default=BROADCAST_IP,
help='The ip address of the host to send the magic packet to.'
' (default {})'.format(BROADCAST_IP))
parser.add_argument(
'-p',
metavar='port',
type=int,
default=DEFAULT_PORT,
help='The port of the host to send the magic packet to (default 9)')
args = parser.parse_args(argv)
send_magic_packet(*args.macs, ip_address=args.i, port=args.p) if __name__ == '__main__': # pragma: nocover
main()

terminal执行:

python3 WakeOnWan.py -i test.tpddns.cn -p   d8:cb:8a:3f::d2
python3 WakeOnWan.py -i 192.168.1.105 -p   d8:cb:8a:3f::d2

C++ 代码:

#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, );
ptr += ; for(i = ; i < ; ++i) {
memcpy(ptr, mac, );
ptr += ;
}
} void usage(void)
{
printf("usage...\n");
} int main(int argc, char **argv)
{
int s;
int packet_num = ;
char c; unsigned char mac[] = {0x00, 0x1A, 0x92, 0xE5, 0x1B, 0xA7};
char dstip[] = "192.168.9.180";
int port = ; struct sockaddr_in address;
char magic_buf[ + * ] = {}; daemon(,); /* run in background */ while((c = getopt(argc, argv, "d:m:p:")) != -) {
switch(c) {
case 'd':
strcpy(dstip, optarg);
break;
case 'm':
sscanf(optarg, "%x:%x:%x:%x:%x:%x",
(unsigned int*)&mac[], (unsigned int*)&mac[],
(unsigned int*)&mac[], (unsigned int*)&mac[],
(unsigned int*)&mac[], (unsigned int*)&mac[]);
break;
case 'p':
port = atoi(optarg);
break;
default:
usage();
return -;
}
} s = socket(AF_INET, SOCK_DGRAM, ); if (s == -) {
perror("Opening socket");
exit(EXIT_FAILURE);
} memset(&address, , 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-- > ) {
if (sendto(s, magic_buf, sizeof(magic_buf), ,
(struct sockaddr *)&address, sizeof(address)) < ) {
printf("sendto\n");
exit(EXIT_FAILURE);
}
sleep();
} exit(EXIT_SUCCESS);
}

注意事项

1: 如果电脑非正常关机(比如按Power键来强制关机)还是无法WOL的,只有正常关机后,网卡仍会处于活动状态可以接收网络数据

2:如果是外网IP访问,路由器做端口映射必须要是顶级接入的路由器,因为二级以下NAT只能在是内网映射

3:Linux设置wol的时候,重启后enp3s0的设置又恢复Wake-on: d 状态, 写个脚本让开机执行 (Ubuntu18.04已修复)

在~/.profile 文件中添加

.config/wol.sh

wol.sh

#!/bin/sh
echo "" | sudo -S ethtool -s enp3s0 wol g

-S表示bash脚本免输密码,参考:  bash脚本,自动输入sudo的密码

网络远程唤醒 WOL Magic Packet的更多相关文章

  1. 网络远程唤醒 WOL Magic Packet【转】

    转自:https://www.cnblogs.com/dcb3688/p/4608063.html Magic Packet Magic Packet白皮书介绍: The basic technica ...

  2. magic packet 远程唤醒需填写 IP broadcast address

    之前摸索过电脑,知道hp compaq6910p有远程唤醒功能的.当时没在意.如今忽然有了实际的需求,就想起来折腾一下了.看了网上的做法,主要是双方面设置,BIOS和网卡.之后就能够用magic pa ...

  3. 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree

    // 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree // 题意:n个点的树,每个节点有权值为正,只能用一次,每条边有负权,可以 ...

  4. 2016中国大学生程序设计竞赛 - 网络选拔赛 C. Magic boy Bi Luo with his excited tree

    Magic boy Bi Luo with his excited tree Problem Description Bi Luo is a magic boy, he also has a migi ...

  5. 对比网络模拟器软件——Cisco Packet Tracer、华为eNSP、H3C Cloud Lab

    1.软件介绍 1.1 Cisco Packet Tracer Cisco Packet Tracer(以下简称PT)是一款由思科公司开发的,为网络课程的初学者提供辅助教学的实验模拟器.使用者可以在该模 ...

  6. 2018ACM/ICPC 焦作网络预选赛-A Magic Mirror

    Jessie has a magic mirror. Every morning she will ask the mirror: 'Mirror mirror tell me, who is the ...

  7. ACM-ICPC 2018 焦作赛区网络预赛 A Magic Mirror(签到)

    https://nanti.jisuanke.com/t/31710 题意 若输入的是Jessie或jessie,输出Good guy!,否则输出Dare you say that again? 分析 ...

  8. 远程唤醒、WOL、Magic_Packet

    背景:很多人熟悉远程桌面并经常地利用它所带来的方便,但是前提是服务器(远程电脑)必须是处于开机状态.对于机房里有专人管理的服务器,这点不成问题,但如果是放在家里的电脑,要让它7*24地开机似乎就不好办 ...

  9. 远程唤醒、WOL、Magic_Packet【转】

    转自:https://www.cnblogs.com/zhuimengle/p/5898830.html 原文:http://blog.csdn.net/flyoxs/article/details/ ...

随机推荐

  1. Gym - 101982C Contest Setting (动态规划)

    A group of contest writers have written n problems and want to use k of them in an upcoming contest. ...

  2. 第二周博客作业<西北师范大学|李晓婷>

    1.助教博客链接:https://home.cnblogs.com/u/lxt-/ 2.点评作业内容: https://www.cnblogs.com/dxd123/p/10494907.html#4 ...

  3. RPC-dubbo基本使用

      22.本地存根 消费者通过创建实现一个服务接口的实例,可以在执行远程调用前拿到远程调用的代理实例,进而可以在远程调用前.后添加一些操作,在出现异常后进行一些容错处理. 这个使用场景,可以调用前作数 ...

  4. Python——爬取人口迁徙数据(以腾讯迁徙为例)

    说明: 1.迁徙量是腾讯修改后的数值,无法确认真实性. 2.代码运行期间,腾讯迁徙未设置IP屏蔽和浏览器检测,因此下段代码仅能保证发布近期有效. 3.代码功能:爬取指定一天的四十(此四十是根据自己的城 ...

  5. Redis的集群搭建

    一.集群的搭建 1.准备工作 (1)安装ruby环境 redis集群管理工具redis-trib.rb依赖ruby环境,首先需要安装ruby环境: yum -y install ruby yum -y ...

  6. idea2017.2.2注册

    IntelliJ IDEA 2017.2 破解(注册) 2017年08月10日 11:13:45 筱进GG 阅读数:2849更多 个人分类: IntelliJ IDEA   下载IntelliJ ID ...

  7. [转] GloVe公式推导

    from: https://pengfoo.com/post/machine-learning/2017-04-11 GloVe(Global Vectors for Word Representat ...

  8. windows环境安装phantomjs和pyspider遇到的问题

    1. 安装phantomjs 下载地址:http://phantomjs.org/download.html 解压后将phantomjs.exe文件放到python根目录 2.安装pyspider p ...

  9. python获取设备主机名和IP地址

    import socket def print_machine_info(): host_name = socket.gethostname() ip_address = socket.gethost ...

  10. 从头开始学gradle【各系统安装gradle】

    所有的环境都是基于jdk1.8 java -version windows安装 下载对应的gradle.zip安装包,解压到指定文件即可,然后配置相应的环境变量即可使用 linux/mac 安装 mk ...