Awk scripts for reading and editing Ubuntu /etc/network/interfaces file (Part 2/2)
For 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
- change a static address properties from an interface defined as static
- 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

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

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.


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





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!
Thanks. I am surprise that it also works on Debian.
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!
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