Simple Bash Script for Pinging Hosts

terminal logo


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

If you’re a complete Bash beginner and have no clue how the ping command works, here’s a simple rundown that will get you up to speed:

What is Ping Command in Bash (Simple Beginners Guide)

Maintaining servers and ensuring websites are up and running is a critical part of owning a website and managing a network. The ping command is one of the most common tools site owners use to check whether all is well with their site. This particular command is conventionally used inside a Bash script. 

Let’s simplify the details and explain why ping is your go-to tool.

What is the Ping Command?

Imagine wanting to check if a friend is available for a chat. So, you ring them up or send them a text message and wait for a reply. 

This is exactly what the ping command does between computers. 

It sends an ICMP echo request, which is essentially like a “hello, are you there?” message to another computer – the server running the website – and waits for a “yes, I’m here!” response.

Syntax of the Ping Command

The basic format of this command is:

ping [options] destination


  • [Ping]: This is the name of the tool we’re using.
  • [options]: These are the extra settings you can adjust when sending your message. Some common ones include:
    • -c: Allows you to specify how many times you want to send the ICMP echo request packets (or “hello” messages).
    • -i: Helps you set the pause time between each packet. It is counted in seconds.
    • -s: Allows you to adjust the size of the packets. It is set to 56 bytes by default on most machines.
    • -t ttl: Think of this as setting limits on how many places your message can go before giving up. A usual default is 64 stops or ‘hops.’
    • -w: Helps you set the maximum wait time for a reply. When this time is exceeded, your machine will assume the other computer didn’t get your message. The default timeout value varies by platform. 
    • -q: This makes the process silent, showing only the final summary.
    • -v: This is the verbose mode, and when active, it displays each packet’s response time. 
  • [destination]: This is who you’re reaching out to. It can be a domain name like or a specific address like

Using Ping to Check Website Status

Run the ping command with your chosen options and destination. It’ll tell you about the number of packets it sent, lost, got responses to, and also the round-trip time of each packet. 

These details help you understand the quality of the connection between your computer and the computer you’re pinging. You will also be able to assess whether the website has a high latency issue or is losing packets. 

Note that different distros have tiny variations in how they use the ping command. If the ping command or its options aren’t working how you’d expect them to on your machine, it’s best to refer to the manual pages on your machine. 

Here’s an example of the ping command in action:




# Let’s try pinging the website

ping -c 1 $WEBSITE > /dev/null

if [ $? -eq 0 ]; then

  echo “$WEBSITE is up!”


  echo “$WEBSITE seems down!”



Here’s what this script is doing:

  1. Setting the Target: We’re defining which website we want to check. Here, it’s “”
  2. Connecting to the site: We use the “ping” command to send ICMP packet (or a friendly “hello”) to the target website. The “-c 1” part means we’ll try just once. The script also instructs the machine not to use the technical details of the packet, and to hide them with > /dev/null.
  3. Getting the Response: After pinging, we check the response. In computer language, we get a “0” if everything went well (meaning the website replied). If we get anything other than “0,” there might be an issue.
  4. Displaying the Result: Depending on the response, our script will either tell us the website is “up” or it “seems down.”

This is a quick way to check website availability, especially useful if you need to make sure your favorite websites, like “,” are running smoothly.