3 # vi: set softtabstop=4 shiftwidth=4 tabstop=8 expandtab:
5 # Creates logfiles to track the nexthop for a list of dynamic routes.
9 echo "usage: $0 -d logdir -r routelist -s snmpconffile" 1>&2
13 # routines for converting a prefix to a netmask
18 prefix
=$
((prefix
- 8*bytenum
))
19 [ $prefix -ge 8 ] && echo 255 && return
20 echo $
((256 - 2**(8-$prefix)))
30 done |
tr '\012' . |
sed 's,.$,\n,'
37 prefix2octets_reversed
()
47 while getopts "d:r:s:" option
51 r
) routelist
=$OPTARG ;;
52 s
) snmpconf
=$OPTARG ;;
57 if [ -n "$routelist" ] && [ -n "$outdir" ] && [ -n "$snmpconf" ]
61 echo "Missing parameters." 1>&2
65 if [ ! -r "$routelist" ]
67 echo "Can't read route list '$routelist'." 1>&2
71 snmpargs
=$
(cat "$snmpconf") ||
{
72 echo "Can't read SNMP argument file '$snmpconf'." 1>&2
78 echo "Output directory '$outdir' does not exist." 1>&2
82 stamp
="$(date +%Y-%m-%d/%H:%M:%S/%s)"
84 while read name router route nexthops
90 # 10.11.7.0/26 -> 10.11.7.0.192.255.255.255
93 revmask
=$
(prefix2octets_reversed
"$prefix")
96 # -Ov: display value only, not OID name
97 # -Oq: display value only, not type
98 nexthop
=$
(snmpwalk
-Ov -Oq $snmpargs "$router" \
99 "IP-FORWARD-MIB::ipCidrRouteNextHop.$target")
101 # Now figure out which nexthop it's using.
102 # 0 for none, 1 for first nexthop, 2 for second, -1 for unexpected nexthop
106 hopdescription
=snmp-failure
107 elif echo "$nexthop" |
grep -q -i "no such instance"
109 # We treat this as the route not existing, but it could also
110 # mean the SNMP instance doesn't have IP-FORWARD-MIB.
112 hopdescription
=route-not-found
115 hopdescription
=$nexthop
118 for candidate
in $nexthops
120 if [ "$candidate" = "$nexthop" ]
129 if ! echo "$stamp $hopcode $hopdescription" >> "$outdir/$name"
131 echo "Can't write to $outdir/$name." 1>&2