Well... It's a <i>litle</i> bit longer, you know.
Code:
let's create the route
grep '^[ \t]*[a-zA-Z]' /etc/dhep/interfaces | while read an_interface_line; do
# Let's find the interface configuration file
length=$(expr length "$an_interface_line")
interfacestart=$(expr index "$an_interface_line" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ)
interfaceend=$(expr index "$an_interface_line" ' ')
if [ $niterfaceend = 0 ]; then
# Only the interface name is written
interfacelength=$(($length-$interfacestart+1))
interface=$(expr substr $an_interface_line $interfacestart $interfacelength)
options=""
else
# Interface and options
interface length=$(($interfaceend-$interfacestart))
interface=$(expr substr "$an_interface_line" $interfacestart $interfacelength)
optionslength=$(($length-$interfacelength+1))
options=$(expr substr "$an_interface_line" $(($interfaceend+1)) $optionslength)
fi
echo $interface Options: $options
# This interfaces configuration
config=/etc/dhep/$interface.cfg
if [ $(grep IP $config | wc -l) != 0 ]; then
# This interface is configured for nexthop
# let's find its IP address
aline=$(cat $config | grep IP)
ip=$(expr substr "$aline" 4 15)
aline=$(cat $config | grep GW)
gw=$(expr substr "$aline" 4 15)
nexthop="nexthop via $gw dev $interface $options $nexthop"
echo $nexthop > /etc/dhep/nexthop.tmp
fi
done
echo nexthop: $nexthop
A little explanation is needed, (I guess).
This piece of script is run as part of a dhclient exit hook. This is used to change the router's multipath route in case a address changes.
The interfaces that are included ar in a file named /etc/dhep/interfaces. in each line of the file is the interface name and options (in case needed), for example: weight:
Here, eth2 is not included for the nexthop evaluation.
And for each interface, the IP address and the GW provided by DHCP are written to a file called /etc/dhep/interface_name.cfg:
Code:
date Tue Jun 28 12:30:44 VET 2005
IP 201.249.120.235
GW 201.249.120.1
By the way... if you think there are better ways to program the script, I'm eager to get them.
Thanks!
(I tried the trick writing down the variable value to a file, and it works!, but I'd like NOT to do it.)