Chinaunix首页 | 论坛 | 博客
  • 博客访问: 90400
  • 博文数量: 30
  • 博客积分: 55
  • 博客等级: 民兵
  • 技术积分: 160
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-21 19:24
文章分类

全部博文(30)

文章存档

2012年(30)

我的朋友

分类:

2012-04-21 19:59:44

可以直接用Linux下面的fping工具代替


说明

  1. 该脚本参考网上资料和一些服务启动的脚本,功能是用于短时间ping大量服务器。
  1. ping通与ping不通的主机IP地址,分别记录在successfailure文件中。
  1. 缺点,暂时还不能打出ping的时间,由于多进程并行,重定向日志会出现混乱,日后改进,可能用数据库保存结果,是一种解决办法
  1. shellscript后只有两个参数,-c是指定并发数,一次ping多少台主机;-f指定IP地址列表文件。
  2. 注释说明,解释一下少见参数的作用

mkfifo $tmp_fifofile      # 新建一个fifo类型的文件

exec 3<>$tmp_fifofile     # 将文件描述符3指向fifo类型

read -u3                  # 此命令执行一次,就从fd3中减去一个回车符,然后向下执行,fd3中没有回车符的时候,就停在这了,从而实现了线程数量控制

echo >&3                  # 当进程结束以后,再向fd6中加上一个回车符,即补上了read -u3减去的那个

wait                      # 等待所有的后台子进程结束

exec 3>&-                 # 关闭df3,文件描述符

  1. 脚本最后,记了ping结果的处理方式

 

#!/bin/bash

# by ninglinajie 20110525

 

nowdate=`date +'%Y%m%d%H%M%S'`

file_fifo="/tmp/$$.pingfifo"

file_ip=$4

file_thread=$2

filename=$0

 

# Use LSB init script functions for printing messages, if possible

# from mysql.server

lsb_functions="/lib/lsb/init-functions"

if [ -f $lsb_functions ]

  then

    . $lsb_functions

  else

    log_success_msg()

    {

      echo " SUCCESS! $@"

    }

    log_failure_msg()

    {

      echo " ERROR! $@"

    }

fi

 

function UsageError()

{

  echo

  echo "$0 : Run Error"

  echo "Usage : Try '$filename -c [n] -f [ipadress file]'"

  echo "        '-c' is not null,and '-f' file is exist "

}

 

if [ $# -eq 0 ]

  then

    UsageError

    exit 1

  else

    if [ $1 = '-c' -a $2 ]

      then

        if [ $3 = '-f' -a -f $4 ]

#'if [ $3 ]' check,not null

          then

            mkfifo $file_fifo

            exec 3<> $file_fifo

            rm -f $file_fifo

#create fifo file

            for ((i=0;i<$file_thread;i++))

              do

                echo

            done >&3

           

            while read serverip

            do

              read -u3

              {

              ping ${serverip} -c 3 -w 3 >/dev/null

              if [ $? -eq 0 ]

                then

                  echo ${serverip} >> pingsuccess_${nowdate}.log

                else

                  echo ${serverip} >> pingfailure_${nowdate}.log

              fi

              echo >&3

              }&

            done < $file_ip

           

            wait

            exec 3>&-

            log_success_msg "$0    is success ($$)"

            exit 0

          else

            UsageError

            log_failure_msg "$0    is failure ($$)"

            exit 1

        fi

      else

        UsageError

        log_failure_msg "$0    is failure ($$)"

        exit 1

    fi

fi

#---------------------------------------------------------------

#ping XXX -c 4 -w 3 |grep rtt|cut -d "/" -f 6

#ping XXX -c 4 -w 3 |gawk -F / '/rtt/ {print $6}' 

#27.631

#ping options description

#-c count

#    Stop after sending count ECHO_REQUEST packets. With deadline option, ping waits for count ECHO_REPLY packets,  until  the  timeout expires.

#-w deadline

#    Specify a timeout, in seconds, before ping exits regardless of how many packets have been sent or received. In this case ping does not  stop  after  count  packet are sent, it waits either for deadline expire or until count probes are answered or for some error notification from network.

#-W timeout

#    Time to wait for a response, in seconds. The option affects only timeout in absense of any responses, otherwise ping waits for two RTTs.

#cut options description

#-d, --delimiter=DELIM

#    use DELIM instead of TAB for field delimiter

#-f, --fields=LIST

#    select only these fields;  also print any line that contains no delimiter character, unless the -s option is specified

阅读(1670) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~