批量测试tcp端口及网络状态--telnet、ping

IP信息列表文件
//cat ip.txt
10.1.1.1
10.1.1.2
……

一、Telnet测试端口状态:
//cat testport.sh

#!/bin/bash
. /etc/init.d/functions
num=0;load=50
read -p "input test port: " por_t
    for i in $(cat ip.txt) ;do 
        (次要内容 num=$((${num}+1)) ;num_seq=`echo $((${num}%50))`
        [ ${num_seq} -eq 0  ] && echo "loading...${load}..." && load=$((${load}+50))  次要内容)
        注:次要内容用于显示速度;每五十个IP提示一次。        
             a=`echo -e "\n"|/usr/bin/telnet ${i} ${por_t} 2>/dev/null |grep -c Connected`
                if [ ${a} -ne 1 ] ;then
                    action "        $i error" /bin/false  
                fi
    done
    echo "end"

二、ping测试网络状态:
1)for循环根据传输的包数判断;
//cat ping1.sh

#!/bin/bash
for i in  $(cat ip.txt) ;do

    ping=$(ping -c1 $i|awk '/loss/{print $4}')

        if [ ${ping} -eq 0 ];then
        echo ping $i fail
        fi
done
echo "ping end"

2)while循环根据返回值判断;
//cat ping2.sh

#!/bin/bash
cat ip.txt|while read line
do

    ping -c1 $line >/dev/null 2>&1 
        if [ $? -ne 0 ];then
        echo "$line fail"
        fi
done
echo "ping end"
                                        ^_^能力有限,欢迎指正~
                                                2018-09-07

你可能感兴趣的:(shell)