Bash 单行命令无需特定工具即可在 LAN 上发送唤醒魔术包

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31588035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:02:49  来源:igfitidea点击:

Bash one-line command to send wake on LAN magic packet without specific tool

bashwake-on-lan

提问by Sopalajo de Arrierez

Is it possible to forge a wake on LAN magic packetand send it in just a one-line bashcommand?

是否可以在 LAN 魔术数据包上伪造唤醒并仅通过一行 bash命令发送它?

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.

当然,我知道有一些特定的工具可以做到这一点,可以在一行中解决问题,但了解 WOL 锻造的最低要求可能会很有用。这就是:如何在没有特定工具的情况下处理 LAN 唤醒。

回答by Sopalajo de Arrierez

The minimum requirementsI can think off:

我能想到的最低要求

  • Bashsupporting brace expansion (I think it is v3.5.1 and above).
  • The sedcommand (1).
  • NetCat.
  • Bash支持大括号扩展(我认为是 v3.5.1 及更高版本)。
  • sed的命令(1)。
  • 网猫

Assuming:

假设:

  • WOL package for LAN, broadcast to 255.255.255.255.
  • LAN 的 WOL 包,广播到 255.255.255.255。

The command linewould be:

命令行是:

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

Replace $MACby the destination MAC. Or, this time in a two-liner :-) command:

替换$MAC为目标 MAC。或者,这次是两行 :-) 命令:

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

Explanations:

说明:

  • The WOL magic packet is composedof ffffffffffff(12 times f) followed by 16 times the destination MAC without colons (:).
  • The sedcommand is used here to remove colons(:) from the MAC and to add the \xhex specificator(so that 11becomes \x11, 22becomes \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. SoCatcan be used instead (syntax will differ, of course).
  • 的WOL魔法包组成ffffffffffff(12倍f),接着由目的地MAC 16次无冒号(:)。
  • sed命令在此处用于从 MAC 中删除冒号( :) 并在将字符串发送到网络堆栈之前添加\x十六进制说明符(以便11变为\x1122变为\x22... 等)。
  • 伪造的 LAN 唤醒包被发送到网络堆栈,通过管道将其传送到 NetCat。可以使用SoCat(当然,语法会有所不同)。

Tested workingon Ubuntu, Kali and even CygWin (Windows 7 SP 1 64 bits ).

测试的工作在Ubuntu,卡利甚至CygWin的(视窗7 SP 1 64位)。

To take under consideration:

考虑到:

  • CygWin's NetCatversion doesn't need for -bparameter.
  • NetCat's OpenBSD version has a bugas 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 port4.000. The specific port numberseems 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 $Broadcastaddress by the destination public IP, and open/forward the specified $PortNumber(UDP) on destination.
  • echo -ecan be replaced by printf.
  • CygWin 的 NetCat版本不需要-b参数。
  • NetCat 的 OpenBSD 版本在今天(2015 年 7 月)广播数据发送 ( ) 上存在一个错误-b,因此您必须将其替换为 NetCat 传统版本(apt-get 安装程序上的 netcat-traditional 包)。
  • 本示例使用UDP 端口4.000。在特定的端口号似乎不是在WOL重要。
  • 上面的一行 bash 命令也应该适用于通过 Internet在 LAN唤醒。在这种情况下,用$Broadcast目标公共 IP替换地址,并在目标上打开/转发指定的$PortNumber(UDP)。
  • echo -e可以用 代替printf

WOL magic packet string for the above example:

上例的 WOL 魔术包字符串:

FFFFFFFFFFFF112233445566112233445566112233445566112233445566112233445566112233445566112233445566112233445566112233445566112233445566112233445566112233445566112233445566112233445566112233445566112233445566

(1) Well, indeed, sedis not explicitly required. It is used here to remove ':' and add \xto each pair of characters in the magic packet's forged string. I know there are ways to replace sedby some shell expansion or so.

(1) 嗯,确实sed没有明确要求。此处用于删除“:”并添加\x到魔术包的伪造字符串中的每对字符中。我知道有一些方法可以用sed一些 shell 扩展来代替。

回答by R4C3R

The default port for the wake-up transmission is UDP port 9.

唤醒传输的默认端口是 UDP 端口 9。

UDP is the recommended protocol to use for WOL because it can be generated without raw sockets which come with security restrictions, and port 9 is recommended because it maps to the old well-known discard protocol. Sometimes you would see port 7 being used but this maps to the echo protocol.

UDP 是用于 WOL 的推荐协议,因为它可以在没有带有安全限制的原始套接字的情况下生成,并且建议使用端口 9,因为它映射到旧的众所周知的丢弃协议。有时您会看到端口 7 正在使用,但这映射到 echo 协议。

This means that if there are hosts on your network that support this old simple standard service you will get unnecessary backscatter traffic when using port 7 but none when using port 9. And since Wake-on-LAN is normally broadcasted, you could get backscatter from many hosts.

这意味着如果您的网络上有支持这种旧的简单标准服务的主机,您在使用端口 7 时将获得不必要的反向散射流量,而在使用端口 9 时则没有。而且由于 LAN 唤醒通常是广播的,因此您可以从许多主机。

Further, if you are troubleshooting WoL with a network sniffer such as Wireshark, it will decode WoL packets properly only if they are UDP packets on port 9.

此外,如果您使用 Wireshark 等网络嗅探器对 WoL 进行故障排除,则只有当它们是端口 9 上的 UDP 数据包时,它才会正确解码 WoL 数据包。

source: https://superuser.com/questions/295325/does-it-matter-what-udp-port-a-wol-signal-is-sent-to

来源:https: //superuser.com/questions/295325/does-it-matter-what-udp-port-a-wol-signal-is-sent-to