-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathripe
executable file
·102 lines (88 loc) · 3.5 KB
/
ripe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env bash
################################################################################
# Global variables.
################################################################################
_NAME="$(command basename "$0")"
_HELP_DOC="Run various RIPE Atlas operations - run traceroute, count probes, get report, etc.
Usage:
$_NAME <destination> [ww|<country>|<asn>] [<probe_count>]
$_NAME (<country>|<asn>)
$_NAME <measurement_link>
$_NAME -h
Options:
-h Print help.
Environment variables:
ATLAS_CREATE_KEY https://atlas.ripe.net/keys"
CURL_ARGS="--connect-timeout 2 --max-time 5"
PROBE_SEARCH_LIMIT=10000
PROBE_COUNT_DEFAULT=10
################################################################################
# Helper methods.
################################################################################
# Usage:
# check_if_installed <binary>
check_if_installed() {
builtin hash "$1" 2>/dev/null || error "please install '$1' ($2)" 69 # EX_UNAVAILABLE
}
# Usage:
# error <message> [<exit_code>]
error() {
[[ $2 -eq 0 ]] && std_err_or_out=1 || std_err_or_out=2
builtin echo "$_NAME: $1" >&"$std_err_or_out"
exit "${2:-1}"
}
################################################################################
# Validate input.
################################################################################
while getopts ":h" arg; do
case $arg in
h) # help
builtin echo "$_HELP_DOC" && exit
;;
*)
builtin echo "$_HELP_DOC" >&2 && exit 64 # EX_USAGE
;;
esac
done
shift $((OPTIND - 1))
[[ $# -eq 0 ]] && error "missing input, please pass a destination, country, ASN, or measurement ID" 64 # EX_USAGE
target="$(command sed -E 's/^asn?([0-9]+)$/\1/g' <<<"${1,,}")"
check_if_installed "jq" "https://github.com/stedolan/jq"
check_if_installed "ripe-atlas" "https://github.com/RIPE-NCC/ripe-atlas-tools"
[[ -z "$ATLAS_CREATE_KEY" ]] && error "missing environment variable, please set ATLAS_CREATE_KEY" 78 # EX_CONFIG
################################################################################
# Execute.
################################################################################
if [[ "$target" =~ ^[a-z]{2}$ || "$target" =~ ^[0-9]{1,10}$ ]]; then # count probes
[[ "$target" =~ ^[a-z]{2}$ ]] && filter_by="country" || filter_by="asn"
command ripe-atlas probe-search --limit "$PROBE_SEARCH_LIMIT" --"$filter_by" \
"$target" --status 1 --ids-only |
command wc -l |
command awk '{print $1}'
elif [[ "$target" =~ ^https://atlas.ripe.net/measurements/[0-9]+/?$ ]]; then # fetch report
target="$(command sed -E 's/.*measurements\/([0-9]+)\/?/\1/g' <<<"$target")"
command curl -qsS $CURL_ARGS -H "Authorization: Key $ATLAS_CREATE_KEY" \
"https://atlas.ripe.net/api/v2/measurements/$target/results" |
command jq -e '.' ||
error "unable to reach atlas.ripe.net"
else # run traceroute
filter_by="area"
filter_value="$(
echo "${2:-WW}" | command tr '[:lower:]' '[:upper:]' | command sed -E 's/^ASN?//g'
)"
if [[ "$filter_value" =~ ^[0-9]+$ ]]; then
filter_by="asn"
elif [[ "$filter_value" =~ ^[A-Z]{2}$ ]] && [[ "$filter_value" != "WW" ]]; then
filter_by="country"
elif [[ "$filter_value" != "WW" ]]; then
error "'$filter_value' is not a valid country or ASN" 65 # EX_DATAERR
fi
probe_count="${3:-$PROBE_COUNT_DEFAULT}"
command ripe-atlas measure traceroute "$target" \
--description "[traceroute] [$filter_value] $target" \
--from-"$filter_by" "$filter_value" \
--probes "$probe_count" \
--resolve-on-probe \
--no-report
fi
# https://atlas.ripe.net/measurements/79403500/