Wake-On-LAN Automation – BASH Script – RaspberryPi

by | Feb 5, 2016 | Debian Wheezy (7)

Wake-On-LAN (WOL) is a very useful feature if you have got a LAB at home and want to make it energy efficient. I have written a small BASH script to automate the Wake-On-LAN (WOL) procedure which can be used in an instance where there is a ESXi server which can turned on as and when required and then shutdown when not needed. Believe me it saves a lot on electric bills as the Servers are not running 24/7 and powered on only when required.

 Make sure you install WOL package on your RaspberryPi by running the below command.

# apt-get install wakeonlan

The below script will work as follows.

  • Check the status of the host via PING (“hppl-1″ is the hostname)
    • If available then send a message its already powered ON.
    • If not,then send a wakeonlan command to the mac address of the host.
    • Wait 3 Minutes (sleep)
    • Again check the status og the host using PING.
    • If available, then send a message on the screen displaying its now powered ON.
    • If PING fails then send a message on the screen its failed and to check the network connections.
#!/bin/bash
VAR=`ping -s 1 -c 2 hppl-1 > /dev/null; echo $?`
if [ $VAR -eq 0 ];then
echo -e "hppl-1 is UP as on $(date)"
elif [ $VAR -eq 1 ];then
wakeonlan 9c:b6:44:98:6d:42 | echo "hppl-1 not turned on. WOL packet sent at $(date +%H:%M)"
sleep 3m | echo "Waiting 3 Minutes"
PING=`ping -s 1 -c 4 hppl-1 > /dev/null; echo $?`
if [ $PING -eq 0 ];then
echo "hppl-1 is UP as on $(date +%H:%M)"
else
echo "hppl-1 not turned on - Please Check Network Connections"
fi
fi 

Please let us know if you come across any issues and we will try to help you out.

Related Articles….