Learn To Write Linux Shell Scripts for Network

0
25076
Linux Shell Scripting 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
1

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.

Syntax

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

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

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

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

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.
6

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:
7

Example
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:
8

Let’s execute it and see it in action:
9

Now, to the explanation:
10

  • 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:

Example
We need to add two entries for the public DNS server 4.2.2.2 and for www.google.com whose IP address is 173.194.65.99

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

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 192.168.1.1 as the DNS server, type the following line in the /etc/resolv.conf file:
12

Example
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:
13

Let’s see it in action:
14

Now, to the explanation:
15

  • 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.

LEAVE A REPLY

Please enter your comment!
Please enter your name here