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


is possible forge wake on lan magic packet , send in one-line bash command?

of course, know there specific tools doing solve problem in 1 line, useful know minimal requirements wol forging. is: how deal wake on lan without specific tools.

the minimum requirements can think off:

  • bash supporting brace expansion (i think v3.5.1 , above).
  • the sed command (1).
  • netcat.

assuming:

  • wol package lan, broadcast 255.255.255.255.

the command line 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 destination mac. or, time in 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 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 composed of ffffffffffff (12 times f) followed 16 times destination mac without colons (:).
  • the sed command used here remove colons (:) mac , add \x hex specificator (so 11 becomes \x11, 22 becomes \x22 ... , on) prior sending string network stack.
  • the forged wake on lan package sent network stack piping netcat. socat can used instead (syntax differ, of course).

tested working on ubuntu, kali , cygwin (windows 7 sp 1 64 bits ).

to take under consideration:

  • cygwin's netcat version doesn't need -b parameter.
  • netcat's openbsd version has bug today (juy 2015) on broadcast data sending (-b), have replace netcat traditional version (netcat-traditional package on apt-get installers).
  • this example uses udp port 4.000. specific port number seems not important on wol.
  • the above one-line bash command should work wake on lan via internet. in case replace $broadcast address destination public ip, , open/forward specified $portnumber (udp) on destination.
  • echo -e can replaced printf.

wol magic packet string above example:

ffffffffffff112233445566112233445566112233445566112233445566112233445566112233445566112233445566112233445566112233445566112233445566112233445566112233445566112233445566112233445566112233445566112233445566 

(1) well, indeed, sed not explicitly required. used here remove ':' , add \x each pair of characters in magic packet's forged string. know there ways replace sed shell expansion or so.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -