Sunday, September 17, 2023

Creating Ping tool that run on Linux using shell script

 



Code below can be created using shell script on a Linux or MAC machines, you can use it to ping list of IP addresses, and you can also add some other features like running the script from time to time and email the result to your business email.

I like using Bash scripts because they require nothing but a Linux machine, unlike Python which requires the installation of the Python and knowledge will be required to know how to write/run the code.

steps:

Open Terminal and use $vi pingtool.sh

#!/bin/bash
#this a ping tool that sends 1 icmp packet to remote host and return is host is up or down
for i in 4.4.2.2 google.com facebook.com bbc.com playstationstore.com  #add more hosts here if needed
do
echo -e "\033[0;36m The Script is Running.......Please wait"
echo -e "\033[0;36m CHECKING $i STATUS"
ping -c 1 $i > /dev/null 2>&1
if [ $? -gt 0 ]; then #if execution failed and return code of the command was not 0 return below message
echo -e "\033[1;31m NOT REACHABLE SITE MIGHT BE DOWN"
else
echo -e "\033[1;32m HOST $i IS UP"
fi
done
echo -e "\033[0;36m &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"
echo -e "\033[0;36m &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"
echo -e "\033[0;36m &&&&&&&&&&& ALL CHECKS ARE DONE &&&&&&&&&&&&"
echo -e "\033[0;36m &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"
echo -e "\033[0;36m &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"


Now, to run the script above, you just need to use the following:
./pingtool.sh
if you face problem with running this then try to give the script permissions to be executable which is by using $chmod u+x pingtool.sh


let's run it:


Note: if you want to edit the host list, then you can vi pingtool.sh and add or remove hosts, save and exit.

No comments:

Post a Comment

Python-Jinja template configuration generator for Cisco devices and printout configs to external text files

 In this post, I worked on collecting a code that works with Jinja template. the nice thing in working with Jinja is that you can have basel...