Encap.txt/get-encap
Here's a bash script to get a complete encap.txt file from the portal. For example:
./get-encap < api-token.txt > encap.txt || echo error $?
The script maintains a cache, so it downloads the information only when it has changed.
#!/bin/bash # Output the current AMPRNet routing information, like this: # # Fetched <when> version <number> from <source> # route addprivate 44.0.1.0/32 encap 169.228.84.34 # route addprivate 44.102.240.16/28 encap 45.145.74.247 # ... cache="${1:-routes.json}" # a file that stores previous information token=`cat -` # an API token # To obtain a token, register with http://portal.ampr.org/ , log in, # browse your profile and copy the value of "API Token". # Be sure to keep the token private. This script won't reveal it. API="https://portal.ampr.org/api/v2" # The API returns JSON, something like: # { # "count": 836, # "serial": "136.269", # "encap": [ # { # "gatewayIP": "118.82.153.200", # "encapType": "IPIP", # "network": "44.147.0.210", # "cidr": 25, # ... # }, # ... # ] # } # ... but without insignificant white space. # We trust that any change will also change the serial value. allZero () { for status in "$@"; do [ "$status" != 0 ] && return "$status" done return 0 } curlOptions="-s -S -f -H \"Authorization: Bearer $token\" -H \"Accept: application/json\" " if [[ -e "$cache" && ! -f "$cache" ]]; then echo >&2 `basename "$0"`": $cache isn't a regular file." exit 1 elif [ -r "$cache" ]; then oldVersion=`jq -e ".serial" "$cache"` || oldVersion="cache error $?" [ "$oldVersion" == "null" ] && oldVersion="N/A" # != $newVersion newVersion=$( echo "$curlOptions" | curl --config - "$API"/encap/serial | jq -e ".serial" allZero "${PIPESTATUS[@]}" || exit $? ) || newVersion="API error $?" else oldVersion="N/A" # != $newVersion newVersion="TBD" fi # echo "# oldVersion=$oldVersion newVersion=$newVersion" if [ "$newVersion" != "$oldVersion" ]; then # Fetch new data from the API and store it in $cache: newVersion=$( echo "$curlOptions" | curl --config - "$API"/encap/routes | tee "$cache" | jq -e ".serial" allZero "${PIPESTATUS[@]}" || exit $? ) || exit $? fi # Convert $cache to the output format: echo "# Fetched" `date -r "$cache"` version "$newVersion" from "$API" || exit $? jq -e -r '.encap[] | select(.encapType == "IPIP") | "route addprivate \(.network)/\(.cidr) encap \(.gatewayIP)"'\ "$cache" | sort # Sorting the output makes it easier to compare different versions. allZero "${PIPESTATUS[@]}" || exit $?