I had a need to edit /etc/network/interfaces file on Ubuntu (from my program). At first I found some awk script on the web, which claimed to do the job, but when I tried, it wasn't addressing all the CRUD cases that I am interested in (also, I didn't want to have system( ) calls from my C code).
So, I searched for a better utility, and found Augeas - this is fantastic! you have to try it to believe! It has a neat command line utility (augtool) as well as an easy to use C API (+ bindings for many scripting languages, including my fav Lua too :-) ).
The following commands show how easy it is to add/rm an interface (XPATH expressions)
So, I searched for a better utility, and found Augeas - this is fantastic! you have to try it to believe! It has a neat command line utility (augtool) as well as an easy to use C API (+ bindings for many scripting languages, including my fav Lua too :-) ).
The following commands show how easy it is to add/rm an interface (XPATH expressions)
$ sudo augtool # Add an interface at the end (last) augtool> set /files/etc/network/interfaces/iface[last()+1] eth1 augtool> set /files/etc/network/interfaces/iface[last()]/family inet augtool> set /files/etc/network/interfaces/iface[last()]/method static augtool> set /files/etc/network/interfaces/iface[last()]/address 10.1.1.1 augtool> save Saved 1 file(s) augtool> $ sudo augtool # Edit the added interface (by name, not position) augtool> set /files/etc/network/interfaces/iface[. = 'eth1']/netmask 255.255.255.0 augtool> save Saved 1 file(s) augtool> set /files/etc/network/interfaces/iface[. = 'eth1']/mtu 1500 augtool> save Saved 1 file(s) augtool> $ sudo cat /etc/network/interfaces auto lo iface lo inet loopback iface eth1 inet dhcp address 10.1.1.1 netmask 255.255.255.0 mtu 1500 $ sudo augtool # Lets just delete eth1 now augtool> rm /files/etc/network/interfaces/iface[. = 'eth1'] rm : /files/etc/network/interfaces/iface[. = 'eth1'] 6 <-- 6="" augtool="" fields="" removed=""> save Saved 1 file(s) augtool> -->Now, the same/similar exercise programmatically: