Making a Simple Ping Test Script

  • Sometimes we just want a simple tool to test ping or similar functions. Don't need anything fancy. So let's do a little BASHing and make a simple script to meet this need. For our example we will make a little ping script that will take a text file as input and ping the hosts and deliver an up or down status on them.

    First we should make a little text file to start testing with. Just open up your editor and put in some IP addresses:

     vi pinglist

    For testing I just used and but you should fill in machines that you care about here. Make sure to put each entry on its own line. You can use IP addresses or Hostnames, but be aware that if you use hostname that you are testing DNS resolution and PING at the same time and if either fails you will get a bad result, so this may or may not make sense for you.

    Now for our script...

    for i in $(cat pinglist); do
        echo $i $(if ping -q -c 1 $i > /dev/null ; then echo up; else echo down; fi)

    Save this file as pinger. Then mark it for execution with chmod +x pinger. Done. Works very simply.