批量检测多主机多端口[tcp/udp]--nc、nmap

  • 时间:
  • 来源:互联网

IP信息列表文件
//cat ip.txt
10.200.6.1
10.200.6.2
……
1)nc
//cat nc.sh

#!/bin/bash
. /etc/init.d/functions
[ ! -f ip.txt ] && echo "find ip.txt fail" && exit 1
menu()
{
cat <<EOF
        ==========================================================================
        a.input port : One or more [0-65535] ;                                   =
                       Eparated ","or"space" ; sample: 22,25,80 or 20 21 111     =
        b.input [tcp/udp] : "t"or"tcp" and "u"or"udp";                           =
        c.port group and [tcp/udp] is true !!                                    =      
        ==========================================================================
EOF
}
menu

read -p "input test port: " por
port=(${por//,/ })
        for j in ${port[@]} ;do
                expr 1 + ${j}  >/dev/null 2>&1
                        if [ $? -eq 0 ] ;then
                                if [ ${j} -gt 65535 -o ${j} -eq 0  ] ;then
                                        echo "$j not ,port in [1-65535];" ;exit 1
                                fi
                        else
                                echo "$j input not number" ;exit 1
                        fi
        done

not_a() {
read -p "input [tcp/udp]or[t/u]: " tu
        if [ "${tu:0:1}" = "t" -o "${tu:0:1}" = "u" ] ;then
                for i in $(cat ip.txt) ;do
                        for j in ${port[@]} ;do
                                a=$(/usr/bin/nc -vz${tu:0:1} ${i} ${j} 2>/dev/null  |grep -c Connection)
                                        if [ ${a} -eq 1 ] ;then
                                                action "                $i $j " /bin/true
                                        else
                                                action "                $i $j " /bin/false
                                        fi
                        done
                done
        else
                echo "input error" ;exit 1
        fi
echo "                  end..."
}
not_a

// sh nc.sh
这里写图片描述

2)nmap
检测多主机udp端口状态:nmap -sU -p111,68 10.1.1.1-20
检查网段主机tcp端口状态:nmap -sT -p22,25 10.1.1.0/24

                                     ^_^能力有限,欢迎指正~
                                            2018-09-10

本文链接http://element-ui.cn/news/show-576693.aspx