可以直接用Linux下面的fping工具代替说明该脚本参考网上资料和一些服务启动的脚本,功能是用于短时间ping大量服务器。将ping通与ping不通的主机IP地址,分别记录在success与failure文件中。缺点,暂时还不能打出ping的时间,由于多进程并行,重定向日志会出现混乱,日后改进,可能用数据库保存结果,是一种解决办法shellscript后只有两个参数,-c是指定并发数,一次ping多少台主机;-f指定IP地址列表文件。注释说明,解释一下少见参数的作用mkfifo $tmp_fifofile # 新建一个fifo类型的文件exec 3$tmp_fifofile # 将文件描述符3指向fifo类型read -u3 # 此命令执行一次,就从fd3中减去一个回车符,然后向下执行,fd3中没有回车符的时候,就停在这了,从而实现了线程数量控制echo >&3 #当进程结束以后,再向fd6中加上一个回车符,即补上了read -u3减去的那个wait #等待所有的后台子进程结束exec 3>&- # 关闭df3,文件描述符脚本最后,记了ping结果的处理方式 #!/bin/bash# byninglinajie 20110525 nowdate=`date+'%Y%m%d%H%M%S'`file_fifo="/tmp/$$.pingfifo"file_ip=$4file_thread=$2filename=$0 # Use LSB initscript functions for printing messages, if possible# frommysql.serverlsb_functions="/lib/lsb/init-functions"if [ -f$lsb_functions ] then . $lsb_functions else log_success_msg() { echo " SUCCESS! $@" } log_failure_msg() { echo " ERROR! $@" }fi functionUsageError(){ 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 fifofile for ((i=0;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 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 fifi#---------------------------------------------------------------#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 optionsdescription#-c count# Stop after sending count ECHO_REQUESTpackets. With deadline option, ping waits for count ECHO_REPLY packets, until the timeout expires.#-w deadline# Specify a timeout, in seconds, before pingexits regardless of how many packets have been sent or received. In this caseping does not stop after count packet are sent, it waitseither for deadline expire or until count probes are answered or for some errornotification from network.#-W timeout# Time to wait for a response, in seconds.The option affects only timeout in absense of any responses, otherwise pingwaits for two RTTs.#cut optionsdescription#-d,--delimiter=DELIM# use DELIM instead of TAB for fielddelimiter#-f,--fields=LIST# select only these fields; also print any line that contains nodelimiter character, unless the -s option is specified 10-07 01:51