Skip to content

Instantly share code, notes, and snippets.

@blizzrdof77
Last active January 17, 2019 21:05
Show Gist options
  • Save blizzrdof77/e2b4307d067667644db7e8bac314f7d9 to your computer and use it in GitHub Desktop.
Save blizzrdof77/e2b4307d067667644db7e8bac314f7d9 to your computer and use it in GitHub Desktop.
Better `ping` support to allow for mixed domains/hostnames w/ protocals (e.g. `ping 'https://who.is/my/ping/?example=this'`)
# ------------------------------------------
# Better `ping` support to allow for mixed
# domains/hostnames with protocols (e.g. 'http://..')
#
# @1 = host
# -> Example usage:
# ping http://google.com/
# ------------------------------------------
function ping {
local pingdomain="$1"
shopt -s nocasematch # allow for lowercase
pingdomain=${pingdomain/#*:\/\/} # strip leading protocol
pingdomain=${pingdomain/#*:*@/} # strip leading user:pass@
pingdomain=$(echo "${pingdomain//"?"//}") # strip trailing '?' onwards
pingdomain="$(echo "$pingdomain" | cut -d/ -f 1)" # strip trailing '/' onwards
if [[ "$#" = "0" ]] && [[ -f $HISTFILE ]]; then
pingdomain=${$(tail -n 1 $HISTFILE)}
pingdomain=${pingdomain/#*ping /}
ping "$(echo $pingdomain)"
else
echo "Pinging hostname \"$pingdomain\""
command ping $pingdomain
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment