In Linux, we can test an internet connection by checking whether a particular website loads up in a browser. However, this isn’t the only method as we can also write shell scripts in Bash to check if the connection is online.
In this article, we’ll take a look at some commands that allow us to test an internet connection and figure out if it’s online.
2. Using the ping Command
ping is the first tool on our list that we’ll utilize for checking the status of an internet connection.
Firstly, let’s create a shell script, file.sh, integrating the ping command:
#!/bin/bash ping -c 1 baeldung.com
Above, we’ve used the ping command to check the connection between our network and the baeldung.com website. In addition to it, we’ve used the -c option, to transmit a particular number of packets, defined after the option.
Secondly, let’s make it executable using the chmod command:
$ chmod +x file.sh
Finally, let’s run the shell script and check the output:
$ ./file.sh PING baeldung.com (126.96.36.199) 56(84) bytes of data. 64 bytes from 188.8.131.52 (184.108.40.206): icmp_seq=1 ttl=57 time=43.0 ms --- baeldung.com ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 42.973/42.973/42.973/0.000 ms
As we can see above, we’ve received the packet that we transmitted. As a result, the packet loss is 0%. This indicates that our internet connection is online.
If the packet loss is between 1-99%, it means that the connection is active, but the network strength isn’t 100%. It’ll get weaker as the packet loss percentage grows. If we get a packet loss of 100%, it indicates that our network has zero connectivity.
3. Using a Combination of cat and echo
The cat command allows us to concatenate files and print the result on the standard output. However, we can also use it in our shell scripts to check port availability:
#!/bin/bash cat < /dev/null > /dev/tcp/www.baeldung.com/53; echo $?
Now, we’ll discuss the logic behind the above shell script.
In Linux, /dev/tcp/<host>/<port> is a device filename where the value of host should be a valid hostname, and the value of port should be a positive integer port number. In our case, the value of host is www.baeldung.com, and the value of port is 53, which are both valid.
As we can see, we’ve inserted the string /dev/tcp/www.baeldung.com/53 after the “>” redirection operator, which instructs Bash to connect to the host www.baeldung.com on port 53.
Now, /dev/null is a null device in Linux that doesn’t store the data that’s written to it. Since we’ve written the string before the “>” operator, the output that Bash generates after trying to connect to the host is written to /dev/null, which it eventually discards. We did this because we wanted to silence the output of Bash.
Next, we can see the cat command is written before the “<” redirection operator, which tells cat to copy content from /dev/null and write it to the terminal. Since /dev/null has nothing in it, cat has copied and printed nothing to the terminal.
Though the cat command isn’t printing anything, it’s generating an exit status based on the connectivity status. To print the exit status, we’ve used the echo command with the $? variable which is essentially telling echo to print the exit status of the previous command, which is cat in our case.
Here is the output of the above shell script:
$ ./file.sh 0
Evidently, the output is 0, which indicates that our network is online.
However, instead of the above output, the below one may appear:
$ ./file.sh -bash: connect: Network is unreachable -bash: /dev/tcp/220.127.116.11/53: Network is unreachable
The above output suggests that we have no connectivity.
4. Using the nslookup Command
nslookup is a useful command for performing a DNS lookup of a particular website. Integrating it into a shell script is simple:
#!/bin/bash nslookup baeldung.com
Let’s see what the output is:
$ ./file.sh Server: 172.20.80.1 Address: 172.20.80.1#53 Non-authoritative answer: Name: baeldung.com Address: 18.104.22.168 Name: baeldung.com Address: 22.214.171.124 Name: baeldung.com Address: 2606:4700:3108::ac42:28f8 Name: baeldung.com Address: 2606:4700:3108::ac42:2b08
If we get an output similar to the above one that contains the data for a given site, it tells us that our network is online.
However, the output can be the one below as well:
$ ./file.sh ;; connection timed out; no servers could be reached
Evidently, the output is unable to show us any data at all, which indicates that our connection is offline.
5. Using the curl Command
curl is a tool that helps us transfer data from or to a server. In addition, we can also make use of it in our shell scripts to test an internet connection:
#!/bin/bash curl -Is http://www.baeldung.com | head -n 1
Let’s understand it step by step:
- -I option, short for –head, allows us to fetch only the HTTP header.
- -s option, short for –silent, puts curl in silent mode.
- -n option, short for –netrc, enables user authentication.
- head -n 1 command allows us to restrict the HTTP header in the output to a certain number of lines, defined after the -n option.
Here’s the output:
$ ./file.sh HTTP/1.1 200 OK
The HTTP status of 200 OK indicates that the connection is online. If we don’t get any output, it signifies that we’re not connected to the internet.
6. The telnet Command
We can also use telnet to check the port connectivity status. Here’s a shell script using the telnet command:
#!/bin/bash telnet 126.96.36.199 53
Let’s have a look at the output:
$ ./file.sh Trying 188.8.131.52... Connected to 184.108.40.206.
Evidently, we’re connected to the internet.
However, we may get the below output instead:
$ ./file.sh telnet: connect to address 220.127.116.11: Network is unreachable
The above output indicates that the connection is offline.
7. The nmap Command
nmap is a port scanning tool that we can use to check the connection status. But before we use it, we’ve got to install it on our machine, as it’s not an in-built tool.
Let’s use apt to install this tool:
$ sudo apt install nmap
Once the installation is finished, we’re ready to use nmap.
Now, let’s put it into a shell script:
#!/bin/bash nmap -p 443 google.com
Above, the -p option is used to tell nmap to scan only the specified ports.
Here’s the output:
$ ./file.sh Starting Nmap 7.80 ( https://nmap.org ) at 2023-06-06 21:50 IST Nmap scan report for google.com (18.104.22.168) Host is up (0.049s latency). Other addresses for google.com (not scanned): 2404:6800:4007:82a::200e rDNS record for 22.214.171.124: maa03s45-in-f14.1e100.net PORT STATE SERVICE 443/tcp open https Nmap done: 1 IP address (1 host up) scanned in 0.17 seconds
The Host is up phrase that we see above indicates that we’re connected to the internet.
We can get the below output as well:
$ ./file.sh Starting Nmap 7.80 ( https://nmap.org ) at 2023-06-06 21:53 IST Failed to resolve "google.com". WARNING: No targets were specified, so 0 hosts scanned. Nmap done: 0 IP addresses (0 hosts up) scanned in 10.05 seconds
Above, we see the Failed to resolve phrase, which means that our internet connection is offline.
8. The nc Command
Another port scanning tool that we can use on our shell scripts to check the connection is nc (or netcat). Here’s an example:
#!/bin/bash nc -vz google.com 443
In the above script, the -v option tells nc to provide more verbose output. On the other hand, we’ve used the -z option to make nc only scan for listening daemons without sending any data to them.
Now, let’s see what the output is:
$ ./file.sh Connection to google.com 443 port [tcp/https] succeeded!
Evidently, our network has successfully connected to the host. Hence, we can understand that our internet connection is online.
The output can also be the below one:
$ ./file.sh nc: getaddrinfo for host "google.com" port 443: Temporary failure in name resolution
We can see that our network has failed to connect to the host. This means that our internet connection is offline.
9. Using the wget Command
wget is a tool that allows us to download files from the internet in a non-interactive manner. However, we can also use it in a shell script to check the connectivity status:
#!/bin/bash wget -q --spider http://google.com ; echo $?
Above, the -q option, short for –quiet, helps us to turn off the output of wget. Additionally, the –spider option makes wget act like a web spider where it just checks if our network can connect to the given site, avoiding the process of downloading the page.
Now, let’s have a look at the output:
$ ./file.sh 0
If we get the above output, we can say that our connection is online. However, anything other than 0 in the output indicates that we’re not connected to the internet.
In this article, we’ve gone through some useful commands that we can use in our shell scripts to test an internet connection.
We noticed that every command seems to generate a different output. However, all of them help us achieve the same goal – determining the status of an internet connection.