Skip to content

November 1, 2009

1

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

by Joe Kuan

ubuntulogoIt is pretty straightforward to setup permanent network configuration on Ubuntu 8.04. All you need is to edit /etc/network/interfaces file and a very thorough tutorial is given in http://www.cyberciti.biz/faq/setting-up-an-network-interfaces-file/ to show you how manually edit for DHCP, static and promiscuous mode. However, I cannot find any command line utility for configuring permanent network setup.

So here are couple awk scripts for reading and writing /etc/network/interfaces file. I hope someone will find it useful, especially for integrating with other programs.

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

Read the interfaces file

Here is a script of readInterfaces.awk to report what has been setup for a network device

BEGIN { start = 0;

    if (ARGC < 3 || ARGC > 4) {
        print "awk -f readInterfaces.awk <interfaces file> device=<eth device> [debug]"
        exit 1;
    }

    for (i = 2; i < ARGC; i++) {
        if (ARGV[i] == debug) {
            debug = 1;
            continue;
        }
        split(ARGV[i], arg, "=");
        if (arg[1] == "device")
            device = arg[2];
    }

    if (!length(device)) {
        print "awk -f readInterfaces.awk <interfaces file> device=<eth device> [debug]"
        exit 1;
    }
}

{
    # Look for iface line and if the interface comes with the device name
    # scan whether it is dhcp or static or manual
    # e.g. iface eth0 inet [static | dhcp | manual]
    if ($1 == "iface")  {
        # Ethernet name matches - switch the line scanning on
        if ($2 == device) {
            if (debug)
                print $0;
            # It's a DHCP interface
            if (match($0, / dhcp/)) {
                print "dhcp";
                exit 0;
                # It's a static network interface. We want to scan the
                # addresses after the static line
            } else if (match ($0, / static/)) {
                static = 1;
                next;
            } else if (match ($0, / manual/)) {
                print "manual";
                exit 0;
            }

            # If it is other inteface line, switch it off
            # Go to the next line
        } else {
            static = 0;
            next;
        }
    }

    # At here, it means we are after the iface static line of
    # after the device we are searching for
    # Scan for the static content
    if (static) {

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

        if ($1 == "address") {
            address = $2;
            gotAddr = 1;
        }
        if ($1 == "netmask") {
            netmask = $2;
            gotAddr = 1;
        }
        if ($1 == "gateway") {
            gateway = $2;
            gotAddr = 1;
        }
    }
}

END {
    if (gotAddr) {
        printf("%s %s %s\n", address, netmask, gateway);
        exit 0;
    } else {
        exit 1;
    }
}

The following is an example of the interfaces file. (Tested with /etc/init.d/networking restart)

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
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

Here are the outputs
interfaces output

Networking 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

1 Comment Post a comment
  1. Mad Willie Thompson
    Jun 23 2011

    Thanks! This is great and exactly what I was looking for. Much appreciated.

    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

%d bloggers like this: