Skip to content

Instantly share code, notes, and snippets.

@discorev
Created May 28, 2021 18:09
Show Gist options
  • Save discorev/36b2ae7d170b6c1e8767ea990a7d68ff to your computer and use it in GitHub Desktop.
Save discorev/36b2ae7d170b6c1e8767ea990a7d68ff to your computer and use it in GitHub Desktop.
Modified rfc3442 classes routes script that includes removing old routes
# set classless routes based on the format specified in RFC3442
# e.g.:
# new_rfc3442_classless_static_routes='24 192 168 10 192 168 1 1 8 10 10 17 66 41'
# specifies the routes:
# 192.168.10.0/24 via 192.168.1.1
# 10.0.0.0/8 via 10.10.17.66.41
RUN="yes"
if [ "$RUN" = "yes" ]; then
# clean old static routes
if [ "$reason" = "RENEW" -o "$reason" = "REBIND" -o "$reason" = "EXPIRE" ]
then
if [ -n "$old_rfc3442_classless_static_routes" ]
then
if [ "$old_rfc3442_classless_static_routes" != "$new_rfc3442_classless_static_routes" ]
then
set -- $old_rfc3442_classless_static_routes
while [ $# -gt 0 ]; do
net_length=$1
via_arg=''
case $net_length in
32|31|30|29|28|27|26|25)
net_address="${2}.${3}.${4}.${5}"
gateway="${6}.${7}.${8}.${9}"
shift 9
;;
24|23|22|21|20|19|18|17)
net_address="${2}.${3}.${4}.0"
gateway="${5}.${6}.${7}.${8}"
shift 8
;;
16|15|14|13|12|11|10|9)
net_address="${2}.${3}.0.0"
gateway="${4}.${5}.${6}.${7}"
shift 7
;;
8|7|6|5|4|3|2|1)
net_address="${2}.0.0.0"
gateway="${3}.${4}.${5}.${6}"
shift 6
;;
0) # default route
net_address="0.0.0.0"
gateway="${2}.${3}.${4}.${5}"
shift 5
;;
*) # error
return 1
;;
esac
# take care of link-local routes
if [ "${gateway}" != '0.0.0.0' ]; then
via_arg="via ${gateway}"
fi
# remove route
vtysh -c "configure terminal" -c "no ip route ${net_address}/${net_length} ${via_arg} ${interface}" >/dev/null 2>&1
done
fi
fi
fi
if [ "$reason" = "BOUND" -o "$reason" = "RENEW" -o "$reason" = "REBOOT" -o "$reason" = "REBIND" ]
then
if [ -n "$new_rfc3442_classless_static_routes" ]
then
set -- $new_rfc3442_classless_static_routes
while [ $# -gt 0 ]; do
net_length=$1
via_arg=''
case $net_length in
32|31|30|29|28|27|26|25)
net_address="${2}.${3}.${4}.${5}"
gateway="${6}.${7}.${8}.${9}"
shift 9
;;
24|23|22|21|20|19|18|17)
net_address="${2}.${3}.${4}.0"
gateway="${5}.${6}.${7}.${8}"
shift 8
;;
16|15|14|13|12|11|10|9)
net_address="${2}.${3}.0.0"
gateway="${4}.${5}.${6}.${7}"
shift 7
;;
8|7|6|5|4|3|2|1)
net_address="${2}.0.0.0"
gateway="${3}.${4}.${5}.${6}"
shift 6
;;
0) # default route
net_address="0.0.0.0"
gateway="${2}.${3}.${4}.${5}"
shift 5
;;
*) # error
return 1
;;
esac
# take care of link-local routes
if [ "${gateway}" != '0.0.0.0' ]; then
via_arg="via ${gateway}"
fi
# set route (ip detects host routes automatically)
vtysh -c "configure terminal" -c "ip route ${net_address}/${net_length} ${via_arg} ${interface}" >/dev/null 2>&1
done
fi
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment