Current iproute2 based gateways "munge" script
Friday, 02 February, 2007, 22:45 PST

Here is my current version of the shell script to process the gateways
(encap.txt) file for updating an iproute2-based
Linux AMPRNet gateway. I call it "iproute2_ampr_munge."
This is a spin-off of the "tunnel-munge" script by N8FOW and
updated by VE3TIX.
I removed code not needed for iproute2 systems. I also
tweaked it so that it would take command-line "add" or "del" arguments,
to create a file that would add new routes, or delete old ones, as
needed. In this manner you can create a flush file, which you
can use to remove your old AMPRNet routes from the table before
updating with the new encap.txt file.
I actually incorporate execution of this script from another one (not shown) that
manages archival of old files (in
case something bad happens to the encap.txt file), and
updates the kernel routing table. It works good for me.
YMMV.
You may download the code below from here. Enjoy!
To do: Try to figure out
a way for awk to output string values of variables, so I can condense
the add_routes
and del_routes
functions into one and re-use the code. (in my free time, of
course)
#!/bin/sh
# iproute2_ampr_munge v20070201
#
# Gateways "munge" script, *only* compatible with iproute2.
# "ip" must be in your path, or the full path entered below.
# Place YOUR gateway's IP in the MYGATE variable.
#
# 01 February, 2007, WA7V:
# Edited commenting for public consumption. ;-)
# 19 March, 2003, WA7V:
# Cleaned up unneeded lines from ifconfig(8) and
route(8), leaving only
# ip(8) commands for inserting routes into the
kernel routing table.
# Added shell functions to take an "add" or "del"
command-line argument,
# and process the command while reusing as much
code as possible.
# Info on iproute2:
http://linux-net.osdl.org/index.php/Iproute2
#
# Significant portions and/or concepts were by:
# Michael Taylor, VE3TIX
# Ron Atkinson, N8FOW
# Bdale Garbee, N3EUA
# Usage: Gateway file on stdin, Linux iproute2 format file on stdout.
# Requires "add" or "del" argument to create appropriate file.
# eg. $0 add < encap.txt > ampr_add
# $0 del < encap.txt
> ampr_del
MYGATE="11.22.33.44"
VERSION="20070201"
do_header() {
echo "#"
echo "# IP tunnel route table built by $LOGNAME on `date`"
echo "# Using $0 v. $VERSION"
echo "# Excluding [local] routes handled by $MYGATE from this file..."
echo "#"
echo "# Remote routes:"
}
do_footer() {
echo "#"
echo "# Default via mirrorshades.ucsd.edu:"
echo "ip route $WHAT 44.0.0.0/8 via 128.54.16.18 dev tunl0 onlink"
echo "#"
echo "# <eof>"
}
add_routes() {
fgrep encap | grep "^route" | grep -v " ${MYGATE}" | \
awk '{
split($3, s, "/")
split(s[1], n,".")
if (n[1] == "") n[1]="0"
if (n[2] == "") n[2]="0"
if (n[3] == "") n[3]="0"
if (n[4] == "") n[4]="0"
#
if (s[2] == "32" || s[2] == "")
printf "ip route add %s.%s.%s.%s via %s dev tunl0 onlink\n"\
,n[1],n[2],n[3],n[4],$5
#
else
printf "ip route add %s.%s.%s.%s/%s via %s dev tunl0 onlink\n"\
,n[1],n[2],n[3],n[4],s[2],$5 }'
#
}
del_routes() {
fgrep encap | grep "^route" | grep -v " ${MYGATE}" | \
awk '{
split($3, s, "/")
split(s[1], n,".")
if (n[1] == "") n[1]="0"
if (n[2] == "") n[2]="0"
if (n[3] == "") n[3]="0"
if (n[4] == "") n[4]="0"
#
if (s[2] == "32" || s[2] == "")
printf "ip route del %s.%s.%s.%s via %s dev tunl0 onlink\n"\
,n[1],n[2],n[3],n[4],$5
#
else
printf "ip route del %s.%s.%s.%s/%s via %s dev tunl0 onlink\n"\
,n[1],n[2],n[3],n[4],s[2],$5 }'
#
}
#
case "$1" in
'add')
WHAT=add
do_header
add_routes
do_footer
;;
'del')
WHAT=del
do_header
del_routes
do_footer
;;
*)
echo "Usage: $0 add|del < infile >
outfile"
echo ""
echo "e.g. $0 add < encap.txt >
ampr_add"
echo " $0
del < encap.txt > ampr_del"
echo "\"> outfile\" is optional.
Default sends to stdout."
echo ""
exit 1
esac
# <EOF>