Last active
August 23, 2020 17:52
-
-
Save Fusl/6a27b3d5bbdf0acdd87ae73d8344359a to your computer and use it in GitHub Desktop.
Ping a target IPv4 address using all mtr.sh probes worldwide sorted by average RTT
This file contains 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 | |
target="${1}" | |
if [ "x${target}" == "x" ]; then | |
echo "Usage: ${0} <IPv4/6 address>" | |
exit 1 | |
fi | |
#if echo -n "${target}" | grep -qE '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$'; then | |
# ip_proto=4 | |
#fi | |
#if echo -n "${target}" | grep -q ':'; then | |
# ip_proto=6 | |
#fi | |
if echo -n "${target}" | grep -q ':'; then | |
ip_proto=6 | |
else | |
ip_proto=4 | |
fi | |
probes_list=$(curl -s https://mtr.sh/probes.json | jq --arg ip_proto "${ip_proto}" -r 'keys[] as $k | if (.[$k].status == true and (.[$k].caps.ping == true or .[$k].caps.ping == ($ip_proto | tonumber))) then $k + "\t" + .[$k].unlocode + "\t" + .[$k].country + "\t" + .[$k].city + "\t" + .[$k].provider else "" end' | grep -vE '^$') | |
probes_count=$(echo "${probes_list}" | wc -l) | |
echo -ne "Tracing: ${target} [$(printf '%*s\n' ${probes_count} '')]\rTracing: ${target} [" 1>&2 | |
results=$( | |
echo "${probes_list}" | awk -F'\t' '{print $1}' | xargs -n1 -P0 -I% sh -c 'echo "% "$(curl -s "https://mtr.sh/%/mtr/'${target}'" | base64 -w0 || echo -n "ZmFpbGVkCg=="); echo -n = 1>&2' | while read probe_id results; do | |
echo "${probe_id} "$(echo "${results}" | base64 -d | wc -l)" ${results}" | |
done | sort -nk2 | while read probe_id sort results; do | |
echo "${probes_list}" | awk -vprobe_id="${probe_id}" '$1 == probe_id' | |
echo "${results}" | base64 -d | |
done | |
) | |
echo | |
echo "${results}" |
This file contains 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 | |
set -euo pipefail | |
target="${1}" | |
if [ "x${target}" == "x" ]; then | |
echo "Usage: ${0} <IPv4/6 address>" | |
exit 1 | |
fi | |
#if echo -n "${target}" | grep -qE '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$'; then | |
# family=4 | |
#fi | |
#if echo -n "${target}" | grep -q ':'; then | |
# family=6 | |
#fi | |
if echo -n "${target}" | grep -q ':'; then | |
family=6 | |
else | |
family=4 | |
fi | |
probes_list=$(curl -s https://mtr.sh/probes.json | jq --arg family "${family}" -r 'keys[] as $k | if (.[$k].status == true and .[$k].caps.ping != false and .[$k]["status" + $family] == true) then $k + "\t" + .[$k].unlocode + "\t" + .[$k].country + "\t" + .[$k].city + "\t" + .[$k].provider else "" end' | grep -vE '^$') | |
probes_count=$(echo "${probes_list}" | wc -l) | |
echo -ne "Pinging: ${target} [$(printf '%*s\n' ${probes_count} '')]\rPinging: ${target} [" 1>&2 | |
runtime=$(date +%s%N) | |
results=$( | |
echo "${probes_list}" | awk -F'\t' '{print $1}' | xargs -n1 -P0 -I% sh -c 'echo "% "$(curl -m60 -s "https://mtr.sh/%/ping/'${target}'" | fgrep -e "packets transmitted," -e "rtt min/avg/max/mdev" || echo "failed"); echo -n = 1>&2' | sort -t/ -rnk5 | while read probe_id results; do | |
echo "${probes_list}" | awk -vprobe_id="${probe_id}" -vresults="${results}" '$1 == probe_id {print $0"\t"results}' | |
done | |
) | |
runtime="$((($(date +%s%N)-${runtime})/1000000))" | |
average=$( | |
echo "${results}" | cut -f6 | tr '/' ' ' | awk "-vruntime=${runtime}" ' | |
{ | |
transmitted+=$1 | |
received+=$4 | |
if ($19 != "") { | |
num+=1 | |
if (min == "" || min > $17) min=$17 | |
sum+=$18 | |
if (max == "" || max < $19) max=$19 | |
std+=$20 | |
} | |
} | |
END { | |
loss = sprintf("%.0f", (transmitted - received) / transmitted * 100); | |
avg = sprintf("%.0f", (sum / num) * 1000) / 1000; | |
std = sprintf("%.0f", (std / num) * 1000) / 1000; | |
print transmitted" packets transmitted, "received" received, "loss"% packet loss, time "runtime"ms rtt min/avg/max/mdev = "min"/"avg"/"max"/"std" ms" | |
} | |
' | |
) | |
echo 1>&2 | |
( | |
echo "${results}" | |
echo -e "avg\tavg\tAverage\tAverage\tAverage\t${average}" | |
) | column -ts$'\t' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output: