#!/bin/sh PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin # The list of Google networks. src="https://www.gstatic.com/ipranges/goog.json" dir="/var/db/google" dest="$dir/goog.json" netlist="$dir/nets" temp="$dest.$$" set -e install -d "$dir" [ "$1" = "-f" ] && rm -f "$dest" "$netlist" trap 'rm -f "$temp" "$netlist.new"' EXIT # Download the list with time modification check. [ -e "$dest" ] || touch -t 197001010000 "$dest" fetch --no-verify-peer -qai "$dest" -o "$temp" "$src" 2>/dev/null [ -s "$temp" ] # The list has been updated. mv "$temp" "$dest" # Get plain list of IPv4 prefixes out of JSON awk -F'"' '/ipv4Prefix/ {print $4}' "$dest" | sort -u > "$netlist.new" [ -e "$netlist" ] || touch "$netlist" cmp -sz "$netlist" "$netlist.new" && exit 0 # Apply changes. gw=$(route -n get default | awk '/gateway: / {print $2}') [ -n "$gw" ] diff "$netlist" "$netlist.new" | { echo 'conf t' sed -Ee '/^[<>]/!d' \ -e 's/^>(.+)/ip route\1 '${gw}/ \ -e 's/^<(.+)/no ip route\1 '${gw}/ echo end } | vtysh 2>&1 | logger -t applynets -p user.info mv $netlist.new $netlist