Skip to content

October 30, 2015

Awk script for changing Ubuntu network interfaces – added DNS feature

by Joe Kuan

At last, I have added the DNS feature for the network interfaces awk script. This feature is only used with static network settings.

Assume we have the following settings in the interfaces file:

auto lo
iface lo inet loopback

auto p3p1
iface p3p1 inet static
    address  192.168.202.194
    netmask  255.255.255.0
    network 192.168.202.0
    gateway  192.168.202.254
    broadcast 192.168.202.255

To add an DNS entry to an existing static IP setting:

Joes-MacBook-Pro-2:~ joe$ awk -f changeInterface.awk /etc/network/interfaces dev=p3p1 dns=8.8.8.8

auto lo
iface lo inet loopback

auto p3p1
iface p3p1 inet static
    address  192.168.202.194
    netmask  255.255.255.0
    network 192.168.202.0
    gateway  192.168.202.254
    broadcast 192.168.202.255
    dns-nameservers  8.8.8.8

To overwrite the DNS setting with multiple addresses

Joes-MacBook-Pro-2:picoLCD joe$ awk -f changeInterface.awk interfaces dev=p3p1 dns='192.168.200.5 8.8.8.8'

auto lo
iface lo inet loopback

auto p3p1
iface p3p1 inet static
    address  192.168.202.194
    netmask  255.255.255.0
    network 192.168.202.0
    gateway  192.168.202.254
    broadcast 192.168.202.255
    dns-nameservers  192.168.200.5 8.8.8.8

To update a new static IP address and preserve other settings:

Joes-MacBook-Pro-2:~ joe$ awk -f changeInterface.awk interfaces.dns dev=p3p1 address=192.168.202.198

auto lo
iface lo inet loopback
 
auto p3p1
iface p3p1 inet static
    address  192.168.202.198
    netmask  255.255.255.0
    network 192.168.202.0
    gateway  192.168.202.254
    broadcast 192.168.202.255
    dns-nameservers  192.168.200.5 8.8.8.8

To remove the DNS entry:

Joes-MacBook-Pro-2:picoLCD joe$ awk -f changeInterface.awk interfaces.dns dev=p3p1 dns=''

auto lo
iface lo inet loopback
 
auto p3p1
iface p3p1 inet static
    address  192.168.202.198
    netmask  255.255.255.0
    network 192.168.202.0
    gateway  192.168.202.254
    broadcast 192.168.202.255

You can checkout the source from my github.

Read more from Awk, Programming

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: