Skip to content

November 1, 2009

15

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.

[ Modified 28/Nov/14 ] – Created a new github repository for this script

[ Modified 22/May/14 ] – Added feature to remove an interface definition

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 if (pair[1] == "mode" && pair[2] == "remove")
            remove = 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;
    }
}
 
{
    if ($1 == "auto" && remove) {
        gsub(device, "");
        print;
        next;
    }

    # 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;
 
            # If remove is defined, switch on delete mode 
            if (remove) {
                definedRemove=1;
            }

            # It's a DHCP interface, if defined any static properties
            # change it to static
            else 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;
            definedRemove = 0;
        }
 
        if (!definedRemove) {
            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;
    }
 
    if (!definedRemove) {
        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
  3. remove an existing interface definition

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

Usage 3. Remove an interface definition

screen

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

15 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
  6. Maksim
    May 22 2014

    Very nice script, but how to remove interface with this script?

    Reply
    • Joe Kuan
      May 22 2014

      I have updated the script to support that feature. Check the blog.

      Reply
      • Maksim
        May 23 2014

        Thanks for changes. Another question about ADD intreface to config file can it add new ip as alias for given interface? Or just add ip as primary?

      • Maksim
        May 27 2014

        Seems like script completely ignore ip if interface not exist in config file which mean impossible add any alias IP.
        What I mean if set device=eth0 mode=add address=1.1.1.1 create
        iface eth0:0
        address 1.1.1.1
        And if put device=eth0 mode=add address=2.2.2.2 it does
        iface eth0:1
        address 2.2.2.2
        Is this possible?

  7. Oct 28 2014

    Hi Joe, Have you put this on any public repo – like GitHub etc? its good to upload it so that its maintained and evolves.

    Reply
    • Oct 29 2014

      [missed this] Also, put a liberal license (like BSD)

      Reply
      • Joe Kuan
        Nov 1 2014

        Thank you for your interest. I will keep that in mind and put it on github once I have a moment.

  8. Jack
    Oct 31 2014

    Hello, your script is very useful, but what if one needs to setup dns servers too?

    Reply
    • Joe Kuan
      Nov 1 2014

      Currently, it doesn’t do that. I don’t use that script for quite sometime, so I haven’t put much improvement to it. Will consider it when I have a moment.

      Reply
      • Jun 3 2015

        Please do. Although I could just as well write something to edit resolve.conf which is much easier to parse.

Leave a reply to Maksim Cancel reply

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

Subscribe to comments