[SlugBug] Routing table question

Chris J cej at nightwolf.org.uk
Mon Aug 14 18:12:58 BST 2006


> 
> > route del default gw 192.168.0.5 netmask 0.0.0.0 dev eth1 && route del -net 169.254.0.0 netmask 255.255.0.0 dev eth1 && route add default gw 192.168.1.1 netmask 0.0.0.0 dev eth0 && route
> 
> but it doesn't always work so i cannot set this as a cron job.  ideally 
> i'd like to just copy a working route table into live and not have to 
> think about it any more - can this be done?
> 

With a little bit of awk, you can dump a routing table to a file, then
read the file back in. To dump:

#!/bin/sh
## The tail +3 skips the first two lines -- "Kernel IP routing
## table" and the column headers
route -n | tail +3 | awk '
$4 == "UH" {
    print "-host", $1, "metric", $5, "dev", $8
}

$4 == "U" {
    print "-net", $1, "metric", $5, "dev", $8
}

$4 == "UG" {
    print "default", "gw", $2, "metric", $5, "dev", $8
}
'
## End of script. copy, paste, chmod 700 (or whatever)


Run the script, redirecting the output to file, then you can
read the contents back in, something like:

#!/bin/sh
cat my_saved_routes | while read a b c d e f g
do
    route add $a $b $c $d $e $f $g
done
## End of script


Now the awk script could be refined quite a bit, but it should give you
a starting point from which to work with :-)

Enjoy,

Chris...

-- 
\ Chris Johnson           \
 \ cej at nightwolf.org.uk    ~-----,   
  \ http://cej.nightwolf.org.uk/  ~-----------------------------------, 
   \ Redclaw chat - http://redclaw.org.uk - telnet redclaw.org.uk 2000 \____


More information about the SlugBug mailing list