Linux批量网络端口测试

需要配置SSH免密

測試網絡和端口的

┌──[root@vms152.liruilongs.github.io]-[~/soft]
└─$echo quit | timeout --signal=9 5 telnet 192.168.26.23 8080
Trying 192.168.26.23...
已杀死
┌──[root@vms152.liruilongs.github.io]-[~/soft]
└─$echo quit | timeout --signal=9 5 telnet 192.168.26.83 8080
Trying 192.168.26.83...
Connected to 192.168.26.83.
Escape character is '^]'.
Connection closed by foreign host.
┌──[root@vms152.liruilongs.github.io]-[~/soft]
└─$echo quit | timeout --signal=9 5 telnet 192.168.26.83 8040
Trying 192.168.26.83...
telnet: connect to address 192.168.26.83: Connection refused

使用方式

┌──[root@vms152.liruilongs.github.io]-[~/soft]
└─$sh network_test.sh "timeout -s 9 4 ping -c 5"
┌──[root@vms152.liruilongs.github.io]-[~/soft]
└─$sh network_test.sh "echo quit | timeout --s9 5 telnet" "8080"

部署方式

远程

┌──[root@vms152.liruilongs.github.io]-[~/soft]
└─$cat  target_list
192.168.26.81
192.168.26.82
192.168.26.83

本地

┌──[root@vms152.liruilongs.github.io]-[~/soft]
└─$cat host_list
192.168.26.152
192.168.26.153
┌──[root@vms152.liruilongs.github.io]-[~/soft]
└─$
┌──[root@vms152.liruilongs.github.io]-[~/soft]
└─$cat network_test.sh
#!/bin/bash

#@File    :   network_test.sh
#@Time    :   2022/10/19 16:45:53
#@Author  :   Li Ruilong
#@Version :   1.0
#@Desc    :   None
#@Contact :   1224965096@qq.com
              # sh network_test.sh "ping -c 5"
              # sh network_test.sh "echo quit | timeout -s 9 5 telnet"

#$cat host_list
#192.168.26.152
#192.168.26.153
#$cat target_list
#192.168.26.81:8080
#192.168.26.83:8080
#

if [ -n "$1" ]
then
 echo "执行的命令为: $1"
else
 echo "需要输入执行的命令"
 echo 'sh network_test.sh "ping -c 5"'
 echo 'sh network_test.sh "echo quit | timeout -s 9 5 telnet"'
 exit 1
fi

for IP in $( cat host_list )
do

if [ -n IP ];then

    echo -e "\n\n \033[31m ##################################### $IP #################################\033[0m"
    for TARGET in $(cat target_list )
    do
    #TYPE=$(echo $1 | grep "ping" )
    if [ -n TARGET ];then
        echo -e "\n\033[31m################ $IP =====>> $TARGET ######################\033[0m"
            serve=$(echo $TARGET | awk -F: '{print $1}')
            port=$(echo $TARGET | awk -F: '{print $2}')
            if [[ $1 == ping* ]] ;then
                echo -e "\033[31m [root@$IP]-[~]#  $1 $serve  \033[0m"
                ssh   root@$IP  $1 $serve
            else
                echo -e "\033[31m [root@$IP]-[~]#  $1 $serve $port \033[0m"
                ssh   root@$IP  $1 $serve $port
            fi
    fi
    done

fi
done
┌──[root@vms152.liruilongs.github.io]-[~/soft]
└─$
posted @ 2022-10-21 15:13  山河已无恙  阅读(197)  评论(0编辑  收藏  举报