Syntax:
The “ping” command can use three types of options for different purposes which are mentioned in the following:
Option | Purpose |
-c | It is used to set the number of packets that are sent to the particular IP address or host. |
-f | It is used to send the maximum number of packets that are allowed by the network. |
-i | It is used to set the interval between two packets in seconds. |
Different Examples of the “Ping” Command
The different ways of using the “ping” command in the Bash script are shown in this part of the tutorial.
Example 1: Check an IP Address Using the “Ping” Command
Create a Bash file with the following script that takes an IP address from the user. The “ping” command is used with the -c option to check whether the IP address is active or inactive. If any error occurs during the execution of the command, it is printed in the terminal. If the IP address exists and is working, the “if” statement returns true.
#Take a valid IP address
echo -n "Enter a valid IP address:"
read ip
#Check whether the taken IP address is active or inactive
if ping -c 2 $ip > /dev/null 2>&1; then
echo "$ip address is live."
else
echo "$ip address is not reachable."
fi
The following output appears after executing the script and running the “ping -c 1 98.137.27.103” command. The output of the “ping” command shows that the IP is active and 1 packet is transmitted and received successfully:
Example 2: Check a Domain Using the “Ping” Command
Create a Bash file with the following script that takes the domain name from the user. The “ping” command is used with the -c option to check whether the domain is active or inactive. If any error occurs during the execution of the command, it is printed in the terminal. If the domain name exists and is working, the “if” statement returns true.
#Take a valid domain name
echo -n "Enter a valid domain name:"
read domain
#Check whether the taken domain is active or inactive
if ping -c 2 $domain > /dev/null 2>&1; then
echo "$domain is live."
else
echo "$domain is unreachable."
fi
The following output appears after executing the script and running the “ping -c 1 youtube.com” command. The output of the “ping” command shows that the domain name is active and 1 packet is transmitted and received successfully:
Example 3: Check Multiple IP Addresses Using the “Ping” Command
Create a Bash file with the following script that checks two IP addresses. The “ping” command is used with the -c option to check whether the IP addresses are active or inactive. If any error occurs during the execution of the command, it is printed in the terminal.
#Define an array of IP addresses
ipArray=("142.250.189.238" "98.137.27.103")
#Iterate the array to check whether each IP address is active or inactive
for ip in "${ipArray[@]}"; do
if ping -c 3 $ip > /dev/null 2>&1; then
echo "$ip is active."
else
echo "$ip is inactive."
fi
done
The following output appears after executing the script and running the “ping” command two times to check whether the IP addresses are active or inactive. The output of the “ping” command shows that two IP addresses are active:
Example 4: Check the Series of IP Addresses Using the “Ping” Command
Create a Bash file with the following script that checks the series of IP addresses by using the “for” loop and the “ping” command.
#Iterate the loop 5 times to check 5 IP addresses
for ip in $(seq 4 8); do
#Check whether the IP address is active or inactive
if ping -c 1 199.223.232.$ip > /dev/null 2>&1; then
echo "199.223.232.$ip is alive."
fi
done
The following output appears after executing the previous script. Here, the 199.223.232.4, 199.223.232.4, 199.223.232.4, and 199.223.232.4 IP addresses are checked and two IP addresses are shown as active:
Conclusion
The uses of the “ping” command in the Bash script to check one or more IP addresses and domain names are shown in this tutorial using multiple examples. The uses of the -c option are shown in this tutorial. The basic uses of the “ping” command are cleared after reading this tutorial.