Skip to content

November 1, 2009

5

Awk scripts for reading and editing Ubuntu /etc/network/interfaces file (Part 2/2)

by Joe Kuan

ubuntulogoFor modifying the /etc/network/interfaces file, I use another script and it is slightly more complicated.

Edit the interfaces file

Here is the awk script, changeInterface.awk for modifying the interfaces file.

function writeStatic(addr, nw, nm, gw) {
    if (length(addr))
        print "    address ", addr

    if (length(nw))
        print "    network ", nw

    if (length(nm))
        print "    netmask ", nm

    if (length(gw))
        print "    gateway ", gw
}

function usage() {
        print "awk -f changeInterfaces.awk <interfaces file> device=<eth device> \n" \
            "       [address=<ip addr>] [gateway=<ip addr>] [netmask=<ip addr>]\n" \
            "       [network=<ip addr>] [mode=dhcp|static] [arg=debug]"
}

BEGIN { start = 0;

    if (ARGC < 3 || ARGC > 9) {
        usage();
        exit 1;
    }

    for (i = 2; i < ARGC; i++) {
        split(ARGV[i], pair, "=");
        if (pair[1] == "address")
            address = pair[2];
        else if (pair[1] == "gateway")
            gateway = pair[2];
        else if (pair[1] == "network")
            network = pair[2];
        else if (pair[1] == "netmask")
            netmask = pair[2];
        else if (pair[1] == "device")
            device = pair[2];
        else if (pair[1] == "arg" && pair[2] == "debug")
            debug = 1;
        else if (pair[1] == "mode" && pair[2] == "dhcp")
            dhcp = 1;
        else if (pair[1] == "mode" && pair[2] == "static")
            static = 1;
        else {
            usage();
            exit 1;
        }
    }

    # Sort out the logic of argument
    if (dhcp && (length(network) || length(gateway) || length(address) || length(netmask))) {
        print "Both DHCP and static properties are defined";
        usage();
        exit 1;
    }
}

{
    # Look for iface line and if the interface comes with the device name
    # scan whether it is dhcp or static
    if ($1 == "iface")  {

        # Ethernet name matches - switch the line scanning on
        if ($2 == device) {

            if (debug)
                print $0;

            # It's a DHCP interface, if defined any static properties
            # change it to static
            if (match($0, / dhcp/)) {
                definedDhcp=1;
                # Change to static if defined properties
                if (length(address) || length (gateway) ||
                    length(netmask) || length (network) || static) {
                    print "iface", device, "inet static";
                    next;
                }
            }

            # It's a static network interface
            else if (match ($0, / static/)) {
                definedStatic=1;
                # Change to dhcp if defined
                if (dhcp) {
                    sub(/ static/, " dhcp");
                    print $0;
                    next;
                }
            }

        }
        # If it is other inteface line, switch it off
        else {
            definedStatic = 0;
            definedDhcp = 0;
        }

        print $0;
        next;
    }

    # Reaches here - means non iface lines
    # Change the static content
    if (definedStatic) {

        # Already defined static, just changing the properties
        # Otherwise omit everything until the iface section is
        # finished
        if (!dhcp) {

            if (debug)
                print "static - ", $0, $1;

            if ($1 == "address" && length(address))
                print "    address ", address

            else if ($1 == "netmask" && length(netmask))
                print "    netmask ", netmask;

            else if ($1 == "gateway" && length(gateway))
                print "    gateway ", gateway;

            else if ($1 == "network" && length(network))
                print "    network ", network;

            else
                print $0;
        }
        next;
    }

    # If already defined dhcp, then dump the network properties
    if (definedDhcp) {
        writeStatic(address, network, netmask, gateway);
        definedDhcp = 0;
        next;
    }

    print $0;
}

END {
    # This bit is useful at the condition when the last line is
    # iface dhcp
    if (definedDhcp)
        writeStatic(address, network, netmask, gateway);
}

Suppose we have the following interfaces file setup

auto lo
iface lo inet loopback

auto eth0 eth1 eth2 eth3

iface eth0 inet static
    address 10.0.11.100
    netmask 255.255.255.0
    gateway 10.0.11.1

iface eth1 inet manual
    up ifconfig $IFACE 0.0.0.0 up
    down ifconfig $IFACE down

iface eth2 inet static
    address 192.168.1.2
    netmask 255.255.255.0
    gateway 192.168.1.254

iface eth3 inet dhcp

Script Usage
The script can

  1. change a static address properties from an interface defined as static
  2. convert an interface with static/manual properties to DHCP and vice versa

Usage 1. Change network static properties

You can define partial or the full set of static properties.

awk -f changeInterface.awk /etc/network/interfaces device=<device> [address=<ip>]  [gateway=<ip>] [netmask=<ip>] [network=<ip>]

Here is a screenshot of an usage example

script usage 1

Usage 2A. Change static/manual interface to DHCP

You can use the script to change static/manual interface to DHCP, simple with mode=dhcp on the target device. The following is a screenshot of the usage

script usage 2A

Usage 2B. Change DHCP interface to static address

The script also works for changing a DHCP network interface into a permanent static address setting, simple specify the static properties.

script usage 2b

Networking

Hopefully, some of you may find this script useful. If you find the script not working probably for your Ubuntu interfaces file, drop me a note with the interface file and I will do my best to fix the script. Thanks.

I work for iTrinegy and here are my other Ubuntu blogs

About these ads
5 Comments Post a comment
  1. Vagelis Koutsomitros
    Dec 29 2009

    I was searching for partial examples with sed and awk, to start from..
    I couldn’t find anything..

    Your script is just awesome!
    Works great on debian!
    Splendid work!

    Reply
  2. Joe Kuan
    Dec 29 2009

    Thanks. I am surprise that it also works on Debian.

    Reply
  3. Vagelis Koutsomitros
    Dec 29 2009

    Don’t be! It is a very basic file.

    Its syntax though is very difficult to parse.

    I have parsed other conf files with basic knowledge of awk and sed. But parsing this one requires deeper knowledge.

    You saved me a lot of time!

    Reply
  4. Sri
    Jan 2 2012

    The output of Usage 2A. Change static/manual interface to DHCP when redirected to file and then eth1 changed to stat with address, netmask and gateway mentioned does not display correctly, only the dhcp is changed to static.

    But when a new line is introduced after line iface eth1 inet dhcp then things start working properly

    Reply
  5. May 28 2013

    looks very good … i made a few changes to also accept dns-nameservers and dns-search settings ;)

    Also noticed that to script it, the best way is probably to add the following bit to the end of the command to make the change to the actual file!
    “> /etc/network/interfaces.tmp && mv /etc/network/interfaces.tmp /etc/network/interfaces”

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments

Follow

Get every new post delivered to your Inbox.

Join 34 other followers

%d bloggers like this: