Wake-On-LAN Automation - BASH Script - RaspberryPi

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.

RasPi Related Articles......

Its now possible to install the latest version of Nagios NRPE v 3.0.1 (source install) on RaspberryPi (incl. Pixel OS).
In Raspberry Pi from version Jessie onwards the "root" login for SSH sessions (via Putty Login) has been disabled by...
If you are getting this error while installing Nagios Core on RaspberryPi run these commands to resolve the error and...
Its now possible to install latest Nagios CORE version on Raspbian by following the steps in this article. Before writing...
Wake-On-LAN (WOL) is a very useful feature if you have got a LAB at home and want to make it...