Check If Host Is Reachable in Bash

Bash check if host is reachable

Using ping Command

Use the ping command in bash to check if the host is reachable or not.

In this bash script, the ping command is used to check if the host "example.com" is reachable or not. In bash, the ping command is used to send ICMP Echo Request packets to the given host and wait for the ICMP Echo Reply. Here, the -c option is used to tell the ping command to send only 1 packet. We can change the value of -c based on the number of packet requests we want to send. Then, > /dev/null; redirected the output to discard it.

The $? is a special variable in bash. It holds the exit status code of the last executed command. In this case, it will capture the exit code of the ping command if it is equal to 0 it will display the message "Host is reachable." on the console and if the exit code is non-zero the message "Host is not reachable." will be displayed. On the execution, of the given command we can see the host example.com is reachable.

Have a look at the below example to see the response of the ping command if the host we are trying to connect is not available.

In the above example, the ping command is used to connect host my.com which is not available on the internet. As a result, the connection failed and the ping command returned the non-zero exit status to an if statement and the message Host is not reachable was displayed on the screen to indicate the connection failure.

Using curl Command:

Use the curl command in bash to check if the host is reachable or not.

In this bash script, the curl command is used to send a HEAD request to check if the specified host "https://www.google.com/" is reachable. The --head option here tells the curl command to send only HTTP HEAD request to the host and the --silent option is used to suppress the progress output. And the --fail option instructed the curl command to send a non-zero status code in the case of request failure to show that the host is not reachable.

On execution of the given script, the message Host is reachable. is displayed on the screen to show that the connection is successfully established.

Note: Replace the "https://www.google.com/" with your desired host in the host variable.

Using nmap Command

Use the nmap command in bash to check if the host is reachable or not.

In this example, the nmap is used to scan the network and check the availability of the given host "example.com" on specified port 80 including the services available on the network. Here, the -p is used to specify the port that we want to scan. The output of the nmap command is then stored in the nmap_output variable.

After that conditional statement is used to check if the nmap_output variable contains an open string indicating that the port is open. In the if condition first the output of echo "$nmap_output" is redirected to the grep command using the pipe | operator as input. Then, grep searched for the string open in the received input. Here, -q is used to suppress the output to only return the exit status.

If the given string is found the exit status code 0 (zero) will be returned and the if block will be executed. On the other hand, if the given string is not found non-zero exit status will be returned and the else block will be executed. On execution of the above code, we can observe that the host "example.com" is reachable at port 80 along with the services available at the network.

Note: Do not forget to install nmap if it is not already installed on your system to avoid errors.

To install nmap run the following command if you are using Ubuntu-based systems/Debian:

And if you are using Windows Operating System download the nmap installer from the official nmap website and follow the given instructions for a successful installation. After installation run the above nmap code to verify if the nmap is installed or not.

Using ncat Command

Use the ncat command in bash to check if the host is reachable or not.

In this bash script, the ncat command is used to check if the host "google.com" is reachable at the given port 443 or not. In bash, the ncat is used to establish the network’s connections and manipulation. The echo -n is used to generate an empty output without a newline character which is passed to the ncat command. Then, ncat attempts to establish a connection with the host at the given port.

Note: The purpose of using echo -n before ncat is to initiate the connection without sending any data, just to test the reachability of the host and port.

Here, the --wait option is used to set the timeout of 1 second for the connection attempt. If the connection is established within the given time ncat will send the exit status 0 to indicate a successful connection. On the other hand, if the connection is not established at the given time ncat will terminate and send the non-zero exit code to indicate connection failure.

In the above case, we can observe that the host "google.com" is reachable at port 443.

That’s all about Bash check if host is reachable.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *