2011年2月16日 星期三

Ping hosts using shell script

#!/bin/sh 
# Author: Frank
ARRAY=( 
  ${USER1} ${USER1IP}\
  ${USER2} ${USER2IP})
SYSTEM=`uname -s`
LOG=/tmp/testping.log.$$
i=0
while [ $i -lt 31 ] ; do
  NAME=${ARRAY[$i]}
  IP=${ARRAY[$i+1]}
    echo -n $NAME $IP ' ' 
    case $SYSTEM in
      Linux)
        ping -c 1 $IP 1> $LOG 2>&1
        ;;
      CYGWIN*)
        ping $IP 64 1 1> $LOG 2>&1
        ;;
      *)
        exit 1
        ;;
      esac
    if [ $? -eq 0 ] ; then
      case $SYSTEM in
        CYGWIN*)
          grep "100.0% packet loss" $LOG 1> /dev/null 2>&1
          if [ $? -eq 0 ]; then
            echo failed
          else
            echo ok
          fi
          ;;
        Linux)
          echo ok
          ;;
        *)
          exit 1
          ;;
        esac
    else
        echo failed.
    fi
    ((i++))
    ((i++))
done
#rm -f $LOG
exit
Reference: Frank

沒有留言:

張貼留言