Linux-Noob Forums

Full Version: for loops and utilizing awk sucessfully for...
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

Ever have to do the same thing over and over again to a batch of files? Ever have to add routes to a group of boxes with different gateways? The list of aplications goes on and on.

lets say you have some zone files that need their serial number incremented. The files are:

 

db.0.0.10.in-addr.arpa

db.1.0.10.in-addr.arpa

db.2.0.10.in-addr.arpa

...

db.255.0.10.in-addr.arpa

 

The frist 10 lines of the files looks like:

 

@ IN SOA some.host.net. dns.host.net. (

2004011401 ; serial

10800 ; refresh (3 hours)

3600 ; retry (1 hour)

604800 ; expire (1 week)

86400 ; minimum (1 day)

)

NS dns.host.net.

 

 

Lets say the serial needs to be incremented to 2004011402 on all zone files due to a mass update. Well doing it manually would be extremely tedius. Here's how I'd do it...

 

cd into the directory of the zone files, then

 

for i in `ls db.*.*.10.in-addr.arpa`

do

sed -e '2,$s/2004011401/2004011402/' > $i.new

cp $i $i.old

mv $.new $i

done

 

You have updated the serial number and kept the original zone files in case you mess up. You should always make backups of a working configuration befor changing it. It can save you hours or work.

 

Example 2:

 

Lets say you have 4 boxes that have to have access to a certain network, but are in differnet datacenters with differnt gateways. I'm going to use netstat output from a Solaris box, it will not matter as your circunstances will allow the following to be adjusted to your environment.

 

foo% netstat -nvr

 

IRE Table: IPv4

Destination Mask Gateway Device Mxfrg Rtt Ref Flg Out In/Fwd

-------------------- --------------- -------------------- ------ ----- ----- --- --- ----- ------

10.23.119.56 255.255.255.248 10.123.119.58 hme0 1500* 0 1 U 685 0

10.64.119.0 255.255.255.0 10.123.119.1 1500* 0 1 U 768 0

224.0.0.0 240.0.0.0 10.184.119.58 hme0 1500* 0 1 U 0 0

default 0.0.0.0 199.184.119.1 1500* 0 1 UG 29088 0

127.0.0.1 255.255.255.255 127.0.0.1 lo0 8232* 0 2 UH 840 0

 

 

lets say the routing table is exactly the same except that the gateway on the boxes to access the said network is 10.123.119.1, 10.123.119.2, 10.123.119.3 and 10.34.123.129.

 

Now we have to set up a route for 128.45.234.0/24 network to access these. Here's a command that could be copied and pasted to all servers:

 

netstat -nvr | grep 10.64.119.0 | tail -1 | awk '{print "route add net 128.45.234.0 -netmask 255.255.255.0 "$3" 1"}' | sh

 

this will create output like:

 

route add net 128.45.234.0 -netmask 255.255.255.0 10.123.119.1 1

 

Adjust for your route syntax.

 

Just find something with the correct gateway in your netstat list, grep for it, tail -1 to ensure it's only 1 line, then awk it and pipe it to sh to execute it. I've used this method to add 12 new routes to 12 servers at the same time. Assing 144 routes by hand would be too tedius to do. Your mileage my vary.

 

-paulpas