Using ping
Command
Use the ping
command in bash to check if the host is reachable or not.
1 2 3 4 5 6 7 8 9 10 |
#!/bin/bash host="example.com" ping -c 1 $host > /dev/null if [ $? -eq 0 ]; then echo "Host is reachable." else echo "Host is not reachable." fi |
1 2 3 |
Host is reachable. |
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.
1 2 3 4 5 6 7 8 9 10 |
#!/bin/bash host="my.com" ping -c 1 $host > /dev/null if [ $? -eq 0 ]; then echo "Host is reachable." else echo "Host is not reachable." fi |
1 2 3 |
Host is not reachable |
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.
1 2 3 4 5 6 7 8 9 |
#!/bin/bash host="https://www.google.com/" if curl --head --silent --fail $host >/dev/null; then echo "Host is reachable." else echo "Host is not reachable." fi |
1 2 3 |
Host is reachable. |
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 thehost
variable.
Using nmap
Command
Use the nmap
command in bash to check if the host is reachable or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#!/bin/bash host="example.com" port=80 nmap_output=$(nmap -p $port $host) if echo "$nmap_output" | grep -q "open"; then echo "Host is reachable on port $port." echo "Services available on the network:" echo "$nmap_output" else echo "Host is not reachable on port $port." fi |
1 2 3 4 5 6 7 8 9 10 11 |
Host is reachable on port 80. Services available on the network: Starting Nmap 7.94 ( https://nmap.org ) at 2023-07-18 12:12 Pakistan Standard Time Nmap scan report for example.com (93.184.216.34) Host is up (0.22s latency). PORT STATE SERVICE 80/tcp open http Nmap done: 1 IP address (1 host up) scanned in 0.96 seconds |
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:
1 2 3 4 |
sudo apt-get update sudo apt-get install nmap |
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.
1 2 3 4 5 6 7 8 9 10 |
#!/bin/bash host="google.com" port=443 if echo -n | ncat --wait 1 $host $port; then echo "Host is reachable on port $port." else echo "Host is not reachable on port $port." fi |
1 2 3 |
Host is reachable on port 443. |
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
beforencat
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.