Home » Linux » Gathering network information for Linux Operating System

Gathering network information for Linux Operating System

Last updated May 17, 2021

In this article, we are going to test IPv4’s connectivity and write scripts for it.

Prerequisites

Besides having a terminal open, we need to remember a few concepts:

  • The If..Else condition case in shell scripting
  • IP address of the device
  • curl command must be installed (you can install it by using the following command: sudo apt install curl)

The purpose of this section is to show you how you can check network connectivity.

How to do it

Open a terminal and create the test_ipv4.sh script:

if ping -q -c 1 -W 1 8.8.8.8 >/dev/null; then 
	echo "IPv4 is up" 
else 
	echo "IPv4 is down" 
fi

Now, to test IP connectivity and DNS, create a script called test_ip_dns.sh:

if ping -q -c 1 -W 1 google.com >/dev/null 
then 
	echo "The network is up" 
else 
	echo "The network is down" 
fi

Lastly, create a script called test_web.sh to test web connectivity:

case "$(curl -s --max-time 2 -I http://google.com | sed 's/^[^ ]* *\([0-9]\).*/\1/; 1q')" in 
[23]) echo "HTTP connectivity is up";; 
5) echo "The web proxy won't let us through";; 
*) echo "The network is down or very slow";; 
esac

How it works

  • Execute the script as $ bash test_ipv4.sh. Here, we are checking the connection with the 8.8.8.8  IP address. For that, we use the ping command in the if condition. If the condition is true, we will get the statement written and printed on the screen as an if block. If not, the statement in else will be printed.
  • Execute the script as $ bash test_ip_dns.sh. Here, we are testing the connectivity using the hostname. We are also passing the ping command in the if condition and checking if the network is up or not. If the condition is true, we will get the statement written in an if block that’s printed on the screen. If not, the statement in else will be printed.
  • Execute the script as $ bash test_web.sh. Here, we are testing the web connectivity. We use the case statement here. We are using the curl tool in this case, which is used to transfer data to and from a server.

Related Posts

Creating a lame utility HTTP server in Linux Operating System

In this article, we will discuss the cURL tool in Linux. The cURL tool is used for transferring the data from or to a server. It supports many protocols, and http is one of them. cURL is used to transfer the data from URL. It has so many tricks to offer, such as http...

Finding binary dependencies in Linux Operating System

In this article, we are going to check the executable. We will find out which string is present in it by using the string command.PrerequisitesBesides having a terminal open, make sure you have a binary present in your directory.Find dependencies First, we...

Capturing network traffic headlessly in Linux Operating System

In this article, we are going to learn how to capture traffic. We are going to capture network traffic with a packet sniffer tool called tcpdump. This tool is used to filter or capture TCP/IP packets that are transferred or received over a network.PrerequisitesBesides...

0 Comments

0 Comments

Submit a Comment

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

one + eighteen =

Follow Us

Our Communities

More on Linux

The Ultimate Managed Hosting Platform
Load WordPress Sites in as fast as 37ms!