Encap.txt/get-encap: Difference between revisions

From 44Net Wiki
Jump to navigation Jump to search
W6JMK (talk | contribs)
Created page with "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. <nowiki> #!/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.24..."
 
W6JMK (talk | contribs)
Handle errors returned from the portal API
Line 46: Line 46:
}
}


curlOptions="-H \"Authorization: Bearer $token\"
curlOptions="-s
-S
-f
-H \"Authorization: Bearer $token\"
-H \"Accept: application/json\"
-H \"Accept: application/json\"
"
"
Line 56: Line 59:
     [ "$oldVersion" == "null" ] && oldVersion="N/A" # != $newVersion
     [ "$oldVersion" == "null" ] && oldVersion="N/A" # != $newVersion
     newVersion=$(
     newVersion=$(
         echo "$curlOptions" | curl -s --config - "$API"/encap/serial | jq ".serial"
         echo "$curlOptions" | curl --config - "$API"/encap/serial | jq ".serial"
         allZero "${PIPESTATUS[@]}" || exit $?
         allZero "${PIPESTATUS[@]}" || exit $?
     ) || newVersion="API error $?"
     ) || newVersion="API error $?"
Line 67: Line 70:
     # 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 -s --config - "$API"/encap/routes | tee "$cache" | jq ".serial"
         echo "$curlOptions" | curl --config - "$API"/encap/routes | tee "$cache" | jq ".serial"
         allZero "${PIPESTATUS[@]}" || exit $?
         allZero "${PIPESTATUS[@]}" || exit $?
     ) || exit $?
     ) || exit $?

Revision as of 19:14, 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 ".serial" "$cache"` || oldVersion="cache error $?"
    [ "$oldVersion" == "null" ] && oldVersion="N/A" # != $newVersion
    newVersion=$(
        echo "$curlOptions" | curl --config - "$API"/encap/serial | jq ".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 ".serial"
        allZero "${PIPESTATUS[@]}" || exit $?
    ) || exit $?
fi
# Convert $cache to the output format:
echo "# Fetched" `date -r "$cache"` version "$newVersion" from "$API" || exit $?
jq -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 $?