Bash单行命令无需特定工具即可在LAN魔术包上发送唤醒 [英] Bash one-line command to send wake on LAN magic packet without specific tool
问题描述
是否可以伪造一个唤醒局域网魔术包,并仅通过单线打击命令将其发送?
Is it possible to forge a wake on LAN magic packet and send it in just a one-line bash command?
当然,我知道有专门的工具可以一口气解决问题,但是了解WOL锻造的最低要求可能会很有用.这是:如何在没有特定工具的情况下处理局域网唤醒 .
Of course, I know there are specific tools for doing this that solve the problem in one line, but it could be useful to know the minimal requirements for WOL forging. This is: how to deal with wake on LAN without specific tools.
推荐答案
最低要求,我可以考虑:
- Bash 支持括号扩展(我认为它是v3.5.1及更高版本).
- sed 命令(1).
- NetCat .
- Bash supporting brace expansion (I think it is v3.5.1 and above).
- The sed command (1).
- NetCat.
假设:
- 用于LAN的WOL程序包,广播到255.255.255.255.
命令行为:
echo -e $(echo $(printf 'f%.0s' {1..12}; printf "$(echo $MAC | sed 's/://g')%.0s" {1..16}) | sed -e 's/../\\x&/g') | nc -w1 -u -b 255.255.255.255 4000
用目标MAC替换$MAC
.或者,这次是两行式的:-)命令:
Replace $MAC
by the destination MAC. Or, this time in a two-liner :-) command:
MAC=11:22:33:44:55:66
echo -e $(echo $(printf 'f%.0s' {1..12}; printf "$(echo $MAC | sed 's/://g')%.0s" {1..16}) | sed -e 's/../\\x&/g') | nc -w1 -u -b 255.255.255.255 4000
因此,用一种更通用的符号表示:
So, in a more generic notation:
MAC=11:22:33:44:55:66
Broadcast=255.255.255.255
PortNumber=4000
echo -e $(echo $(printf 'f%.0s' {1..12}; printf "$(echo $MAC | sed 's/://g')%.0s" {1..16}) | sed -e 's/../\\x&/g') | nc -w1 -u -b $Broadcast $PortNumber
说明:
- WOL 魔术包由组成(
ffffffffffff
(f
的12倍),然后是目标MAC的16倍,没有冒号(:
). - 此处使用
sed
命令从MAC中删除冒号(:
),并添加\x
十六进制指定符(这样,变成 \x11
,22
变成\x22
...依此类推),然后再将字符串发送到网络堆栈. - 伪造的LAN唤醒程序包被发送到网络堆栈将其插入NetCat .可以改用 SoCat (语法当然会有所不同).
- The WOL magic packet is composed of
ffffffffffff
(12 timesf
) followed by 16 times the destination MAC without colons (:
). - The
sed
command is used here to remove colons (:
) from the MAC and to add the\x
hex specificator (so that11
becomes\x11
,22
becomes\x22
... and so on) prior to sending the string to the network stack. - The forged wake on LAN package is sent to the network stack piping it to NetCat. SoCat can be used instead (syntax will differ, of course).
在Ubuntu,Kali甚至CygWin(Windows 7 SP 1 64位)上测试了工作.
Tested working on Ubuntu, Kali and even CygWin (Windows 7 SP 1 64 bits ).
要考虑的问题:
- CygWin的NetCat 版本不需要
-b
参数. - NetCat的OpenBSD版本在今天(2015年7月)的广播数据发送(
-b
)上有一个 bug ,因此您必须将其替换为NetCat传统版本(位于上的netcat-traditional软件包) apt-get安装程序.) - 此示例使用 UDP端口 4.000. 特定端口号在WOL上似乎并不重要.
- 上面的单行bash命令对于通过Internet 在LAN上唤醒也应该起作用.在这种情况下,将
$Broadcast
地址替换为目标公用IP,然后在目标上打开/转发指定的$PortNumber
(UDP). -
echo -e
可以替换为printf
.
- CygWin's NetCat version doesn't need for
-b
parameter. - NetCat's OpenBSD version has a bug as for today (Juy 2015) on broadcast data sending (
-b
), so you will have to replace it by NetCat Traditional version (netcat-traditional package on apt-get installers). - This example uses UDP port 4.000. The specific port number seems not to be important on WOL.
- The above one-line bash command should work too for wake on LAN via internet. In this case replace
$Broadcast
address by the destination public IP, and open/forward the specified$PortNumber
(UDP) on destination. echo -e
can be replaced byprintf
.
上例中的WOL魔术包字符串:
WOL magic packet string for the above example:
FFFFFFFFFFFF112233445566112233445566112233445566112233445566112233445566112233445566112233445566112233445566112233445566112233445566112233445566112233445566112233445566112233445566112233445566112233445566
(1)确实,并没有明确要求sed
.它用于删除':'并将\x
添加到魔术包伪造字符串中的每对字符.我知道有一些方法可以通过一些shell扩展替换sed
.
(1) Well, indeed, sed
is not explicitly required. It is used here to remove ':' and add \x
to each pair of characters in the magic packet's forged string. I know there are ways to replace sed
by some shell expansion or so.
这篇关于Bash单行命令无需特定工具即可在LAN魔术包上发送唤醒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!