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

2

40

Is it possible to forge a wake on LAN magic packet and send it in just a one-line bash command?

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.

Hegelianism answered 23/7, 2015 at 12:55 Comment(0)
H
54

The minimum requirements I can think off:

  • Bash supporting brace expansion (I think it is v3.5.1 and above).
  • The sed command (1).
  • NetCat.

Assuming:

  • WOL package for LAN, broadcast to 255.255.255.255.

The command line would 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 $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

Explanations:

  • The WOL magic packet is composed of ffffffffffff (12 times f) 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 that 11 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).

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

To take under consideration:

  • 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 by printf.

WOL magic packet string for the above example:

FFFFFFFFFFFF112233445566112233445566112233445566112233445566112233445566112233445566112233445566112233445566112233445566112233445566112233445566112233445566112233445566112233445566112233445566112233445566

(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.

Hegelianism answered 23/7, 2015 at 12:55 Comment(11)
Would it be possible to use bash's /dev/udp instead of netcat?Begat
I think it is not, but it is under debate here: unix.stackexchange.com/questions/217476/… . In short: you can do it, but there are some unstabilites. I.e: sending \x0a acts as some sort of datagram break flag for the network package, so you can not send (or I don't know how) WOL for MACs like 0a:11:22:33:44:55.Hegelianism
This worked great for use with MobaXTerm's bash session (uses cygwin), thanks!Edessa
I think it's worth mentioning that in certain distributions (such as Ubuntu 16.04), you need to use the netcat-traditional package (Unix) to run this command, and not the ǹetcat-openbsd package, which has different ways to pass parameters. If your system uses the openbsd flavor (like on Ubuntu 16.04) , uninstall it, and install the traditional flavor. To check which flavor you have, run man nc. If the top line is "BSD General Commands Manual", it's the BSD flavor.Industrious
You are right, @Industrious . More details here: unix.stackexchange.com/questions/217683/…Hegelianism
@Industrious - the actual error message when the wrong netcat version is installed is: nc: protocol not supportedElwaine
run into the same trouble with the -b on nc (ubuntu 14.04), but replaced the nc call with a socat call: socat - UDP-DATAGRAM:${Broadcast}:${PortNumber},broadcastVaulting
any reason why I'd get a "nc: bad interface name" error putting in the above command?Dyann
With this note that if you use a MAC containing e.g. 0a, the resulting binaries will fail and WOL will not work. The reason is to use Bash as a generator. Some characters have special meaning for bash, see this thread: stackoverflow.com/questions/62091541Extrude
I tried to put this in a shell script and it didn't work. I had to use #!/bin/bash instead of #!/bin/sh. So remember to use the correct shell or the printfs won't work as described!Surrejoinder
just an FYI to improve the answer: bash has sed substitution built in (goto man bash then parameter expansion then pattern substitution). bash also has means to send udp packets built in via echo helloworld > /dev/udp/hostname/port. so if i had more time i would improve this via getting rid of sed and nc.Alack
D
3

The default port for the wake-up transmission is UDP port 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.

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.

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.

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

Disharmonious answered 8/11, 2018 at 7:7 Comment(3)
From Review: Hi, while links are great way of sharing knowledge, they won't really answer the question if they get broken in the future. Add to your answer the essential content of the link which answers the question. In case the content is too complex or too big to fit here, describe the general idea of the proposed solution. Remember to always keep a link reference to the original solution's website. See: How do I write a good answer?Breastplate
Welcome to StackOverflow. Even when your info about the WOL protocol could be useful, this is not the proper way to write it down, because it does not actually answer the original question. It would be best to delete your answer, as long as it does not really offers a method to forge WOL packets without specific WOL tools. If not, you could be downvoted.Hegelianism
It is an improvement of your answer. You wrote that it doesn’t matter which UDP port is used, but the convention is to use port 9 for WOLDisharmonious

© 2022 - 2024 — McMap. All rights reserved.