Shell ScriptingLearn To Write Linux Shell Scripts for Network

Learn To Write Linux Shell Scripts for Network

“Doesn’t deserve to be born, who live only for himself.” An Arabian Saying.

Computer networking is analogous to what we see in our life. Nobody can live alone, and people really need each others. Similarly, there is no one computer system that could do everything. From this fact the client/server model was developed. In fact, a server is called server because it provides services. To whom? Of course, to others, and sometimes to itself as well.

In this article, we are going to tackle a new topic: how could our shell scripts deal with the network, and use network services?

Managing Network Administrative Tasks for Linux Box
We will start by learning how to manage the basic network administrative tasks for our Linux server.

Displaying the Network Addresses
To display the network configuration for the network interface cards in the Linux box, use the command ip a

In the output, we can easily identify the available network interfaces, and their physical (MAC) addresses, and their logical (IP) addresses.

Bring a Network Interface Up and Down
There will be cases wherein you need to bring a network interface down, and enable it again. For instance, when you change the IP address of a network interface, and instead of restarting the whole network service (that will unnecessarily bring other interfaces down, and bring them up again), you can just shutdown down that interface, then bring it up). For this purpose two commands exist: ifdown, and ifup.



To bring the loopback interface down, use the command ifdown:

To test the status of the interface after being shutdown, use the command ip a

Notice that its status is now DOWN. To enable it, use ifup:
Now, check the status after bringing the interface up:

Displaying Hostname
The command hostname prints the name of the system (hostname).

Changing the Hostname
To change the hostname of your Linux machine, the same command hostname could be used, by providing to it the new name as an argument.

This change is not permanent; i.e. it doesn’t persist after rebooting the machine. To change the hostname permanently, edit the configuration file /etc/sysconfig/network, and set the value for HOSTNAME as follows:

Write a script that accepts a new hostname from the user as a command-line argument, and sets the machine hostname permanently.

Consider the following script:

Let’s execute it and see it in action:

Now, to the explanation:

  • Line 6: retrieves lines from the /etc/sysconfig/network file that don’t contain the pattern HOSTNAME. The retrieved lines are then redirected to a temporary file.
  • Line 7: appends the line “HOSTNAME=$1” to the /etc/sysconfig/network file. Where $1 is first command-line argument (which is expected to be the new hostname to set).
  • Line 8: replaces the /etc/sysconfig/network file with the newly-created tempfile.
  • Line 9: sets the machine hostname (temporarily).
  • Line 10: removes the temporary file.

Learn the Basics of C Programming Language

Resolving Names to IP Addresses
Communications between different hosts is done by IP address. This makes Name Resolution an essential process. Internet for example, would be useless if you were unable to resolve a web site URL to IP address. Also, the Mail system can never work without a Domain Name Server DNS.

To resolve a hostname to its IP address, two methods exist: the primitive method, that depends on the /etc/hosts file, and the advanced (and the recommended as well) method, which is querying a DNS server.

/etc/hosts Method
The /etc/hosts file can help do hostname to IP mapping. It is a simple ASCII file that consists of lines with the following syntax:

host1		x1.y1.z1.w1
host2		x2.y2.z2.w2

We need to add two entries for the public DNS server and for whose IP address is

Edit the /etc/hosts file, and append two lines to the end of the file as follows:

Save and exit the file.

/etc/resolv.conf Method
In this method, we configure our Linux box to be a DNS client that refers to a DNS server (or more) when it needs to make name-to-address resolution.

The key player in this method is the /etc/resolv.conf file. In this file, we specify one or more name servers to consult on need for name resolution.

For example, to set as the DNS server, type the following line in the /etc/resolv.conf file:

Write a script that accepts the hostname and IP address as command-line arguments, and adds them to the /etc/hosts file.

Consider the following script:

Let’s see it in action:

Now, to the explanation:

  • Line 6: searches for the provided IP address (first argument) in the /etc/hosts file.
  • Line 7: checks the exit status of the previous command (in Line 6). If found to be 0, this means that the IP already exists in the file. in this case, Lines 8 and 9 are executed:
    • Line 8: prints an error message to the user notifying him that this IP already exists.
    • Line 9: The exit command quits the program with an exit code of 1.
    • Line 10: fi that closes the if statement.
  • Lines from 11 to 15: repeat the above process for the second command-line argument (hostname).
  • Line 16: if neither of the above conditions succeed, (i.e. both of the provided IP address and hostname doesn’t exist in the file), a new line containing the IP address and the hostname is appended to the end of the /etc/hosts file.

That is it!!!

* * * * * *

In this article, we have talked about writing Linux shell scripts for network. We have learned how to display MAC and IP addresses for the network interfaces, how to bring a network interface up and down, how to get the machine hostname, and how to set it both temporarily and permanently. We have also talked about name resolution, using the local /etc/hosts file, and using the DNS server.
That was part one in the articles on Scripting for Network. So, stay here, part two is on the way to you.


Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article


More article

- Advertisement -