nc批量测试端口连通性

将以下2个脚本放在同一路径下,测试时只需要运行Run_network_test.sh

  • Run_network_test.sh
#!/bin/bash

for ip in `cat hosts | awk '{print $1}'`   #hosts为需要测试的主机列表
do
        echo "                   "
        echo "                   " >> final_result
        echo '-------------------'
        echo '-------------------' >> final_result
    echo $ip 
    echo $ip >> final_result

    sshpass -p "password"  ssh -o StrictHostKeyChecking=no user@$ip bash -s < test_network.sh 

    sshpass -p "password"  ssh -o StrictHostKeyChecking=no user@$ip "cat /tmp_result" >>final_result

    sshpass -p "password"  ssh -o StrictHostKeyChecking=no user@$ip "rm -f /tmp_result" >>final_result
done
  • test_network.sh
#!/bin/bash

#目的主机IP列表
list="192.168.119.117 192.168.119.119"

for ip in $list

do
        nc -z $ip 3066 >/dev/null  #将命令执行结果全部丢弃,无论 nc -z $ip 3066测试结果成功与否,都不影响$?的值。

        if [ $? -eq 0 ];then
                echo "`hostname -i `  -->   $ip is ok "  >>/tmp_result
        else

                echo "`hostname -i `  -->   $ip is bad "  >>/tmp_result
        fi      
done

测试示例

同样的,nc可换成telnet、traceroute等命令。

你可能感兴趣的:(nc批量测试端口连通性)