Created
February 16, 2025 11:59
-
-
Save tonusoo/8967e5f27ec742d10464057275342f91 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Find IPv4 prefixes which have non-unique origin AS number, i.e. the rightmost AS number. | |
get_origin_asn() { | |
last_asn_elem="${1##* }" | |
case "$last_asn_elem" in | |
"{"*"}") | |
# It's an AS-set. | |
last_asn_elem="${last_asn_elem#\{}" | |
last_asn_elem="${last_asn_elem%\}}" | |
last_asn_elem="${last_asn_elem//,/ }" | |
;; | |
esac | |
echo "$last_asn_elem" | |
} | |
while IFS='|' read -r _ _ _ _ prefix as_path _; do | |
if [[ -z "$last_prefix" ]] || [[ "$prefix" == "$last_prefix" ]]; then | |
# Double quotes around the command substitution are missing in order to | |
# add possibly multiple AS-set members as separate array elements. | |
origin_asns+=( $(get_origin_asn "$as_path") ) | |
else | |
mapfile -t unique_origin_asns < <(printf "%s\n" "${origin_asns[@]}" | sort -un) | |
(( ${#unique_origin_asns[@]} > 1 )) && echo "$last_prefix:" "${unique_origin_asns[@]}" | |
origin_asns=() | |
origin_asns+=( $(get_origin_asn "$as_path") ) | |
fi | |
last_prefix="$prefix" | |
done < <(bgpkit-parser --ipv4-only latest-bview.gz) | |
mapfile -t unique_origin_asns < <(printf "%s\n" "${origin_asns[@]}" | sort -un) | |
(( ${#unique_origin_asns[@]} > 1 )) && echo "$last_prefix:" "${unique_origin_asns[@]}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment