Encap.txt/get-encap: Difference between revisions
Jump to navigation
Jump to search
Handle errors returned from the portal API |
handle errors from jq |
||
Line 6: | Line 6: | ||
The script maintains a cache, so it downloads the information only when it has changed. | The script maintains a cache, so it downloads the information only when it has changed. | ||
<nowiki> | <nowiki>#!/bin/bash | ||
#!/bin/bash | |||
# Output the current AMPRNet routing information, like this: | # Output the current AMPRNet routing information, like this: | ||
# # Fetched <when> version <number> from <source> | # # Fetched <when> version <number> from <source> | ||
Line 56: | Line 55: | ||
exit 1 | exit 1 | ||
elif [ -r "$cache" ]; then | elif [ -r "$cache" ]; then | ||
oldVersion=`jq ".serial" "$cache"` || oldVersion="cache error $?" | oldVersion=`jq -e ".serial" "$cache"` || oldVersion="cache error $?" | ||
[ "$oldVersion" == "null" ] && oldVersion="N/A" # != $newVersion | [ "$oldVersion" == "null" ] && oldVersion="N/A" # != $newVersion | ||
newVersion=$( | newVersion=$( | ||
echo "$curlOptions" | curl --config - "$API"/encap/serial | jq ".serial" | echo "$curlOptions" | curl --config - "$API"/encap/serial | jq -e ".serial" | ||
allZero "${PIPESTATUS[@]}" || exit $? | allZero "${PIPESTATUS[@]}" || exit $? | ||
) || newVersion="API error $?" | ) || newVersion="API error $?" | ||
Line 70: | Line 69: | ||
# Fetch new data from the API and store it in $cache: | # Fetch new data from the API and store it in $cache: | ||
newVersion=$( | newVersion=$( | ||
echo "$curlOptions" | curl --config - "$API"/encap/routes | tee "$cache" | jq ".serial" | echo "$curlOptions" | curl --config - "$API"/encap/routes | tee "$cache" | jq -e ".serial" | ||
allZero "${PIPESTATUS[@]}" || exit $? | allZero "${PIPESTATUS[@]}" || exit $? | ||
) || exit $? | ) || exit $? | ||
Line 76: | Line 75: | ||
# Convert $cache to the output format: | # Convert $cache to the output format: | ||
echo "# Fetched" `date -r "$cache"` version "$newVersion" from "$API" || exit $? | echo "# Fetched" `date -r "$cache"` version "$newVersion" from "$API" || exit $? | ||
jq -r '.encap[] | select(.encapType == "IPIP") | "route addprivate \(.network)/\(.cidr) encap \(.gatewayIP)"'\ | jq -e -r '.encap[] | select(.encapType == "IPIP") | "route addprivate \(.network)/\(.cidr) encap \(.gatewayIP)"'\ | ||
"$cache" | sort | "$cache" | sort | ||
# Sorting the output makes it easier to compare different versions. | # Sorting the output makes it easier to compare different versions. | ||
allZero "${PIPESTATUS[@]}" || exit $? | allZero "${PIPESTATUS[@]}" || exit $? | ||
</nowiki> | </nowiki> |
Revision as of 19:42, 15 October 2024
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 any 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 $?