Thursday, April 7, 2011

Solaris Like Ping Bash Script

Here is a simple bash script I wrote to allow you to perform a quick ping operation and have it work similar to how it does on Solaris operating systems:

#!/bin/bash
pinghost()
{
  ping -c 1 $args &> /dev/null
  case $? in
  0)
    echo "$host is alive";
  ;;
  1)
    echo "no answer from $host";
  ;;
  2)
    echo "sping: unknown host $host";
  ;;
  esac
}
if [ -z $1 ]; then
  echo "Usage: sping host"
  echo `ping 2>&1 | sed -e 's/ping/sping/'`
else
  #Get hostname from args (in any order)
  args=$@
  while test -n "$1"
    do
      if [[ "$1" = -* ]]; then
        shift 2
      else
        host="$1"
        break
      fi
      #shift
  done
  pinghost $args
fi

 
Great little tool when pinging lots of machines at once in a script. The original functionality of the ping command is retained (you can run command line arguements). You can even make this script executable and put it in your /usr/bin folder for easy access.

No comments:

Post a Comment