Skip to content

Instantly share code, notes, and snippets.

@netj
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save netj/5d95a999da6088c57f64 to your computer and use it in GitHub Desktop.
Save netj/5d95a999da6088c57f64 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# A script for checking bandwidth of KAIST FTP's new network
# Usage: curl -sL https://gist.github.com/netj/5d95a999da6088c57f64/raw/ftp.kaist.ac.kr-bwtest.sh | bash -s [TIMEOUT] [PATH]
#
# Author: Jaeho Shin <netj@sparcs.org>
# Created: 2014-09-25
set -eu
# ftp.kaist.ac.kr IPs
ipCurrent=143.248.234.110
ipKOREN=103.22.220.133
# add sbin to PATH
PATH="$PATH:/usr/sbin:/usr/local/sbin"
# On Mac, use Homebrew's coreutils without the g-prefixes
! type brew &>/dev/null || ! brew --prefix coreutils &>/dev/null ||
PATH="$(brew --prefix coreutils)/libexec/gnubin:$PATH"
# Check prerequisites
canProceed=true
# XXX checking each command as type builtin of older bash doesn't fail
for prereq in curl traceroute dd pv timeout grep sed tee mktemp rm; do
type $prereq >/dev/null || canProceed=false
done
$canProceed || {
echo "Aborting test as some required commands are not available"
case $(uname) in
Darwin)
type brew &>/dev/null || echo "Hint: Install Homebrew from http://brew.sh and run the following command"
echo "Hint: brew install pv coreutils"
;;
Linux)
if type apt-get &>/dev/null; then
echo "Hint: sudo apt-get install traceroute pv curl"
elif type yum &>/dev/null; then
echo "Hint: sudo yum install traceroute pv curl"
else
echo "Hint: Install traceroute pv curl packages on your system"
fi
;;
esac
exit 2
} >&2
# Record public IP (See: http://www.go2linux.org/what-is-my-public-ip-address-with-linux)
ipPublic=$(curl --silent --show-error checkip.dyndns.org |
sed -e 's/.*Current IP Address: //' -e 's/<.*$//')
bwtest() {
local host=${1:-} timeout=${2:-} path=${3:-}
: ${host:?hostname or IP required}
: ${timeout:=30s}
: ${path:=/debian-cd/current/amd64/iso-cd/debian-7.6.0-amd64-netinst.iso}
local url="http://$host$path"
# Print what's being downloaded from and to where
printf '%s\n' \
'Download speed check of: ' \
$'\t'"$url" \
'Download starting: ' \
$'\t'"$(date +"%FT%T$(date +%:z)")" \
'Downloading from: '
if [ -n "$ipPublic" ]; then
printf '\tPublic IP %s\n' "$ipPublic"
else
# Record interface IPs instead when no public IP is available
/sbin/ifconfig | grep 'inet ' |
grep -vE 'inet (addr:)?(127|169.254|192.168|10|172.(1[6-9]|2[0-9]|3[0-1]))\.'
fi
# Measure download speed with curl and dd with the help of pv and timeout
(
timeout $timeout curl -s $url |
pv --wait --bytes --timer --rate --average-rate
) | dd of=/dev/null 2>&1
}
markdown-codeblock-of() { echo '```'; "$@"; echo '```'; echo; }
open-url() {
local url=$1
# Open URL in default browser if possible
case $(uname) in
Darwin)
open "$url" ;;
Linux) # See: http://stackoverflow.com/questions/5116473/linux-command-to-open-url-in-default-browser
if [ -n "$DISPLAY" ]; then
for cmd in sensible-browser www-browser gnome-open xdg-open
do type "$cmd" >/dev/null || continue
"$cmd" "$url" &
break
done
fi
;;
esac
}
# Measure speeds
log=$(mktemp ${TMPDIR:-/tmp}/speedchk.XXXXXX)
trap 'rm -f $log' EXIT
{
echo 'Produced by running [ftp.kaist.ac.kr-bwtest.sh](https://gist.github.com/netj/5d95a999da6088c57f64):'
echo '```'
echo 'curl -sL https://gist.github.com/netj/5d95a999da6088c57f64/raw/ftp.kaist.ac.kr-bwtest.sh | bash -s'
echo '```'
echo
echo "## Current ISP ($ipCurrent)"
markdown-codeblock-of bwtest $ipCurrent "$@"
markdown-codeblock-of traceroute $ipCurrent
echo; echo "* * * * *"; echo
echo "## KOREN ($ipKOREN)"
markdown-codeblock-of bwtest $ipKOREN "$@"
markdown-codeblock-of traceroute $ipKOREN
} | tee $log
# Post test result to Gist
if ! [ -t 0 ] || read -p "Press Enter to post result to gist, or Ctrl-C to quit"; then
echo "Posting to GitHub Gist anonymously..."
gisturl=$(
{
printf '{'
printf '"description":"ftp.kaist.ac.kr bandwidth test",'
printf '"public":true,'
printf '"files":{"ftp.kaist.ac.kr-bwtest.md":{"content":"'
sed <$log 's/\\/\\\\/g; s/$/\\n/; s/[ ]/\\t/g; s/"/\\"/g' | tr -d '\n'
printf '"}}}'
} | curl --silent --show-error --data @- https://api.github.com/gists |
grep 'html_url' | sed 's/^.*"\(http[^"]*\)".*/\1/'
)
echo $gisturl
open-url $gisturl
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment