2012年(30)
分类:
2012-04-21 19:59:44
原文地址:大规模主机ping的ShellScript 作者:ning_lianjie
可以直接用Linux下面的fping工具代替
说明
mkfifo $tmp_fifofile # 新建一个fifo类型的文件
exec 3<>$tmp_fifofile # 将文件描述符3指向fifo类型
read -u3 # 此命令执行一次,就从fd3中减去一个回车符,然后向下执行,fd3中没有回车符的时候,就停在这了,从而实现了线程数量控制
echo >&3 # 当进程结束以后,再向fd6中加上一个回车符,即补上了read -u3减去的那个
wait # 等待所有的后台子进程结束
exec 3>&- # 关闭df3,文件描述符
#!/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