configure network interfaces with awk
Here a small script which allows to write /etc/network/interfaces cgi style. I`ve written it since I search for a small project to learn a little bit more awk. tested with debian squeeze, report me if it works also with other distributions. |
|
#!/usr/bin/awk -f # example call to set config value: # ./interfaces interface=eth0 address=172.30.30.2 # options: # * address # * netmask # * gateway # * dns # # interface param is required all others are optional # also multible params can given # # example call to get current address value: # ./interfaces get=address interface=eth0 interfaces # # example call to set hostname: # ./interfaces hostname=newname BEGIN { # we use all time the same input file so I define here which file to use ARGV[ARGC] = "/etc/network/interfaces" ARGC = ARGC + 1 } { if(hostname != "") { system("hostname " hostname) print hostname > "/etc/hostname" exit 0 } if(get == "dns") { get_dns() exit 0 } # interface is required its used as selector which we want modify if(interface == "") { exit 1 } # get value from interface configuration if($1 == "iface" && $2 ~ interface && get != "") { get_if_val(get) } line[NR] = $0 # set value(s) to interface configuration # all params are optional if($1 == "iface" && $2 ~ interface && get == "") { cnf_if(address, netmask, gateway, dns) } } # get one value from network interfaces function get_if_val(get) { while($1 != "auto" && getline != 0) { if($1 == get) { print $2 exit 0 } } } # configure interface function cnf_if(address, netmask, gateway, dns) { while ($1 != "auto" && getline != 0) { if($1 == "address" && address != "") { sub(".*", address, $2) } if($1 == "netmask" && netmask != "") { sub(".*", netmask, $2) } if($1 == "gateway" && gateway != "") { sub(".*", gateway, $2) } if($1 == "dns-nameservers" && dns != "") { sub(".*", $1 " " dns, $0) } line[NR] = $0 } # write configuration for(i=1;i<=NR;i++) { print line[i] > "/etc/network/interfaces" } # restart interface system("ifdown " interface) system("ifup " interface) } function get_dns() { while(getline != 0) { if($1 == "dns-nameservers") { sub("dns-nameservers ", "", $0) print $0 exit 0 } } } |
|
| created at | 2011-12-20 19:00:00 |
| updated at | 2011-12-20 19:07:28 |
|
write comment |
| mm |
nice |
2011-12-21 15:06:24 |
1 comments