Introduction to Linux - A Hands on Guide | Linux Bible | Linux From Scratch | A Newbie's Getting Started Guide to Linux | Linux Command Line Cheat Sheet | More Linux eBooks



Wednesday, 2 October 2013

Basic Linux Shell Scripting Language : IP Aliasing


    Introducing the concept of IP Aliasing in one of my previous articles - How To : Create an Alias for a Network Interface Card in Ubuntu/Linux Mint I have written : "The process in which multiple addresses are created on a single network interface, is known as "IP Aliasing". IP Aliasing will be very much useful when you need multiple IP addresses to set up multiple virtual sites on Apache making the use of only one network adapter. The most important advantage of IP Aliasing is that you do not need one hardware per IP Address, rather you can generate a pool of virtual network interfaces (i.e. Aliases) on a single device."

    We have seen, in the said article, how aliases can be created using the 'ifconfig' command. Creating a few aliases may not be difficult, but as the number of aliases rises, the job becomes more and more tiresome and time consuming. As a solution for this, we may make use of BASH Scripts to create desired number of aliases using single network interface card. BASH Scripts are efficient and can be executed on every system reboot.

    In this article, we will assign a pool of IP addresses to a network interface card using a BASH Script. If you are new to Shell Scripting, please read my article - Getting Started : Linux Shell Scripting Language and then go through this one.

Let's create a Script!

First of all, you need to create an empty file with .sh as an extension.

touch Aliases.sh
Edit the file with your favorite text editor.

sudo gedit Aliases.sh
Now, Scripting Time! Let us consider, we have to create 30 aliases. Of course, you can create as many aliases as you want.


#!/bin/bash

MAX=32

for x in $(seq 3 $MAX)
do
    ifconfig eth0:${x} 192.168.186.${x} netmask 255.255.255.0 up
done

This tiny script will create 30 aliases on single network interface card with IP addresses ranging from 192.168.186.3 to 192.168.186.32

Now if you run the 'ifconfig' command, it will produce a very lengthy output like:


eth0:3    Link encap:Ethernet  HWaddr 00:0c:29:85:d6:8e  
          inet addr:192.168.186.3  Bcast:192.168.186.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:19 Base address:0x2000 

eth0:4    Link encap:Ethernet  HWaddr 00:0c:29:85:d6:8e  
          inet addr:192.168.186.4  Bcast:192.168.186.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:19 Base address:0x2000

...

eth0:32   Link encap:Ethernet  HWaddr 00:0c:29:85:d6:8e  
          inet addr:192.168.186.32  Bcast:192.168.186.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:19 Base address:0x2000 

If you want to delete all of them, you can run following script:


#!/bin/bash

MAX=32

for x in $(seq 3 $MAX)
do
    ifconfig eth0:${x} 0.0.0.0 &> /dev/null
done

For the script to be executed on every system reboot, you should edit the /etc/rc.local file and add a line mentioning the path to the script (say /home/user/Desktop/Aliases.sh) to the end of that file. One thing should be noted that, this file might be missing in various distributions, you are advised to consult with distribution's docs to find it's location.

You May Also Like:

1 comment:

  1. Ifconfig is obsolete. Use ip instead it!

    ReplyDelete