Skip to content

Instantly share code, notes, and snippets.

@MartinDelille
Created March 6, 2014 09:45
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 MartinDelille/9386306 to your computer and use it in GitHub Desktop.
Save MartinDelille/9386306 to your computer and use it in GitHub Desktop.
# `pg` with no arguments ping the IP 8.8.8.8 (usefull for basic internet connection test),
# otherwise ping the given IP. If the IP is incomplete, it is concat with the default
# prefix 192.168.1 allowing easy local ping (eg ping 3.12 => 192.168.3.12)
function pg() {
ip4regex='^[0-9]+[.][0-9]+[.][0-9]+[.][0-9]+$'
ip3regex='^[0-9]+[.][0-9]+[.][0-9]+$'
ip2regex='^[0-9]+[.][0-9]+$'
ip1regex='^[0-9]+$'
host=$@
if [[ $# == 0 ]]; then
host="8.8.8.8"
elif [[ $@ =~ $ip4regex ]]; then
host="$@"
elif [[ $@ =~ $ip3regex ]]; then
host="192.$@"
elif [[ $@ =~ $ip2regex ]]; then
host="192.168.$@"
elif [[ $@ =~ $ip1regex ]]; then
host="192.168.1.$@"
fi
ping $host
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment