Simple Bash Script for Pinging Hosts

Simple Bash Script for Pinging Hosts

This is a simple useful script which I learned from “Coding For Penetration Testers” book. It is used to check if a host is alive or not and echoes on user screen a message with the results of the ping command, if the host is up it informs the user “HOST IS UP” with the echo command like shown in Figure 1.

Figure 1

We use the if conditional to test if the host is up and print to the screen a message with the echo command, if the host is not alive we print “HOST IS DOWN”, use an else statement to do it. This is the heart of our script, and it is inside a function called pingcheck. As you can see from Figure 1 we declare a function called pingcheck with the function command. After that, we open and close curly brackets and put our code inside it. The ping variable holds the result of the piping process inside backticks. We use ‘ping -c 1’ to ping our target with a single packet and pipe the result of this command with a pipeline to the grep command and grep the word bytes. After that, we use piping again and send the result of grep command to wc command which stands for word cound. The final result of these commands stringed together is stored in the ping variable which we test in the if statement. The ping variable holds a number, which is the number of word ‘bytes’ returned after ‘ping -c 1 $host’ command is executed. If the value of ping variable is greater than 1 it means that the host has responded to our request, which means the host is alive. The ‘fi’ command inside pingcheck function indicates the end of the if statement. To execute the pingcheck function, we write ‘pingcheck’ outside the function. The first line of this script is the shebang line, which tells the system  what interpreter to use in order to interpret the script. The second line is used to get input from the user and $1 means the first argument. To execute this script, open a terminal and type ‘./ host’, where is the Bash script and host is the first argument. For example run ‘./’ like shown in Figure 2.

Figure 2

The  output  is shown  in  Figure 3.

Figure 3

  • LinuxSytesNet

    Very useful, hoping that in future you will write more tutorials like this :)

  • johnsmith500

    Good except that we need to run the screenshot through an OCR to get the code of the script. ;-)

  • Suresh kumar

    Thanks for your grateful informations, am working in Ecommerce development company india, so it will be helpful info for my works.

  • Panos Georgiadis

    use nmap ;)
    simply enough your host might blocking ICMP echo requests and in that case you need trickier methods to see the actual response, such as sending a SYN packet ;)

  • LinuxSytesNet
  • John Muraguri

    Here is another script I have and use to ping a range of of IPs.

    for i in 192.168.20.{1,11,19,78,100,120,125,127,132}
    ping -c 3 -t 3 “$i” >/dev/null 2>&1 &&
    echo “Ping Status of $i : Success” ||
    echo “Ping Status of $i : Failed”

  • Panos Georgiadis

    Oops, my bad. I meant ACK. I that way, a misconfigured server thinks that he has already established a connection with the client and he maybe respond back.

  • Ambiton

    Thank you very much for the good comments. I will make bash scripts tutorials again.

  • Ambiton

    Thank you :)

  • Ambiton

    Thanks :)

  • Ambiton

    Thank you Panos about mentioning nmap. The purpose of this article is to introduce beginners with bash scripting and how it helps to solve problems. I have an article about nmap :)

  • Ambiton

    Nice John , Thank you very much for sharing your script and your knowledge with us :)

  • Martin Møller Skarbiniks Peder

    You can ping a lot of hosts very fast if you have GNU Parallel and write this:

    seq 1 254 | parallel -j0 ping -c 1 192.168.1.{} | grep “64 bytes”

  • Neaj

    I wrote a french tutorial about a similar script. I use a bash table to have many IP to ping and the script ping each IP one after one to fixe which is down an which is up

    It’s could be handy in a big network to fix with jump cause the problem from a client to internet for example