Awk script for changing Ubuntu network interfaces – added DNS feature

Datetime:2016-08-23 01:05:42          Topic: AWK           Share

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 .





About List