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.

Most Read Articles

Add/Detect a new disk in ...

Written By Farooq Mohammed Ahmed on Sunday, 11 January 2015 19:22
Add/Detect a new disk in ...

Replace SSL Certificates ...

Written By Farooq Mohammed Ahmed on Friday, 16 December 2016 08:05
Replace SSL Certificates ...

Using awk in Alias Comman...

Written By Farooq Mohammed Ahmed on Friday, 19 February 2016 14:22
Using awk in Alias Comman...

Script to Monitor Nagios ...

Written By Farooq Mohammed Ahmed on Monday, 18 July 2016 22:48
Script to Monitor Nagios ...

Generate/Create a SHA2/SH...

Written By Farooq Mohammed Ahmed on Tuesday, 25 October 2016 13:56
Generate/Create a SHA2/SH...