这几天到墨西哥的通信有点问题,导致业务下发大面积失败,为了及时跟墨西哥方面沟通,反馈通信问题,于是写了个检测脚本,把脚本的ip1,port1,count换成自己想要的参数即可

#!/bin/bash
ip1=127.0.0.1
port1=3306
count=10
y=0
z=0
 tel()
{
telnet $ip1 $port1 > /tmp/tel.log 2>&1 &
sleep 3
var1=`awk '/Escape/ {print $1}' /tmp/tel.log`
rm -rf /tmp/tel.log
if [ -z $var1 ]
then
echo -e "telnet $ip1 $port1 \033[31m bad \033[0m "
#y=`expr $y + 1`
y=$((++y))
else
echo "telnet $ip1 $port1 ok"
#z=`expr $z + 1`
z=$((++z))
fi
}
for((i=1;i<=$count;i++))
do
tel
done
echo -e " telnet \033[31m $count \033[0m times to $ip1 $port1"
echo -e " success \033[31m $z \033[0m times,faile \033[31m $y \033[0m times"
percent=`echo "scale=2;$z*100/$count"|bc`
echo -e " success \033[31m $percent% \033[0m "
unset ip1 port1 count percent

 今天抽空,把脚本改了下,改成交互式

#!/bin/bash
#create by dennis.xie 2012-09-06
ip1=
port1=
count=
y=0
z=0
ipinput(){
  read -p "please input the server-ip you want to telnet:" ip1
if [ -z $ip1 ]
then
echo -e "\\033[31m no ip input,please input again \033[0m"
ipinput
elif [[ $ip1 =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]
then
IFS='.'
ip=($ip1)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
if [ $? -eq 0 ]
then
echo
else
echo -e "\\033[31m error ip,please input again \033[0m"
ipinput
fi
else
echo -e "\\033[31m error ip,please input again \033[0m"
ipinput
fi
}
 

port1(){
read -p "please input the port:" port1
if [ -z $port1 ]
then
echo -e "\033[31m no data input,please input again.\033[0m"
port1
elif
expr $port1 + 1 >>/dev/null 2>&1
then
if [ $? -eq 0 ]
then
 echo
else
echo -e "\033[31m error port,please input the port again \033[0m"
port1
fi
else
echo -e "\033[31m error port,please input the port again \033[0m"
 port1
fi
}

count(){
read -p "how many times do you want to telnet:" count
if [ -z $count ]
then
echo -e "\033[31m no data input,please input again.\033[0m"
count
elif
expr $count + 1 >>/dev/null 2>&1
then
if [ $? -eq 0 ]
then
 echo
else
echo -e "\033[31m inpunt error ,please input the times again \033[0m"
count
fi
else
echo -e "\033[31m inpunt error ,please input the times again \033[0m"
count
fi
}


tel()


{


telnet $ip1 $port1 > /tmp/tel.log 2>&1 &


sleep 3


var1=`awk '/Escape/ {print $1}' /tmp/tel.log`


rm -rf /tmp/tel.log


if [ -z $var1 ]


then


echo -e "telnet $ip1 $port1 \033[31m bad \033[0m "


#y=`expr $y + 1`


y=$((++y))


else


echo "telnet $ip1 $port1 ok"


#z=`expr $z + 1`


z=$((++z))


fi


}


ipinput


port1


count


for((i=1;i<=$count;i++))


do


tel


done


echo -e " telnet \033[31m $count \033[0m times to $ip1 $port1"


echo -e " success \033[31m $z \033[0m times,faile \033[31m $y \033[0m times"


percent=`echo "scale=2;$z*100/$count"|bc`


echo -e " success \033[31m $percent% \033[0m "


unset ip1 port1 count percent