Skip to content

Instantly share code, notes, and snippets.

@FlyMyPG
Last active May 21, 2017 23:49
Show Gist options
  • Save FlyMyPG/0f66c35e97c662c52c1c6759a620d386 to your computer and use it in GitHub Desktop.
Save FlyMyPG/0f66c35e97c662c52c1c6759a620d386 to your computer and use it in GitHub Desktop.
#!/bin/bash
###############################################################################
# File: MastoPingParallelSort.bash
#
# Author: BobC https://mastodon.hasameli.com/@BobC
# License: "CC BY-NC-SA 4.0" https://creativecommons.org/licenses/by-nc-sa/4.0/
#
# Requires: GNU Parallel
#
# Find the Mastodon instance closest to you based on average ping time.
#
# This is MUCH faster than the massive one-liner in MastoPingSort.bash.
###############################################################################
get_instances() {
# Send URLs of known Mastodon instances to stdout
echo -e "\nGathering instance URLs." >/dev/fd/2
wget -q -O - 'https://instances.mastodon.xyz/list' | \
grep -i 'scope=' | \
grep -o 'https[^\"]*' | \
sed "s+https://++" | \
tee >(echo -e "Checking $(wc -l) instances.\nPlease allow a few minutes..." >/dev/fd/2)
}
export -f get_instances
pingit() {
# Do up to 10 pings, then output average ping time + URL to stdout
x=$1 # For readability
ping -q -l 1 -i 0.2 -w 2 -c 10 $x 2>/dev/null | \
grep "min/avg/max" | \
cut -d/ -s -f5 | \
echo $(cat -) $x | \
grep "\ "
}
export -f pingit
# GNU Parallel beats xargs: Do 20 pingits in parallel per virtual core
get_instances | parallel --no-notice -j+20 pingit | sort -g -k1,1
@FlyMyPG
Copy link
Author

FlyMyPG commented May 21, 2017

This script works pretty much everywhere bash can be run, including the WSL (Windows Subsystem for Linux) included with Win10-64.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment