批量測試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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章