Array
(
    [query] => Array
        (
            [match] => Array
                (
                    [text] => Array
                        (
                            [query] => Linux shell编写端口扫描脚本
                        )

                )

        )

    [highlight] => Array
        (
            [fields] => Array
                (
                    [text] => stdClass Object
                        (
                        )

                )

            [pre_tags] => #em#
            [post_tags] => #/em#
        )

    [size] => 8
    [from] => 0
)
Array
(
)
Array
(
    [query] => Array
        (
            [match] => Array
                (
                    [text] => Array
                        (
                            [query] => Linux shell编写端口扫描脚本
                            [operator] => or
                            [minimum_should_match] => 2<55%
                        )

                )

        )

    [highlight] => Array
        (
            [fields] => Array
                (
                    [text] => stdClass Object
                        (
                        )

                )

            [pre_tags] => #em#
            [post_tags] => #/em#
        )

    [size] => 8
    [from] => 0
)
Array
(
)
Array
(
    [query] => Array
        (
            [match] => Array
                (
                    [text] => Array
                        (
                            [query] => Linux shell编写端口扫描脚本
                        )

                )

        )

    [highlight] => Array
        (
            [fields] => Array
                (
                    [text] => stdClass Object
                        (
                        )

                )

            [pre_tags] => #em#
            [post_tags] => #/em#
        )

    [size] => 8
    [from] => 8
)
Array
(
)
Array
(
    [query] => Array
        (
            [match] => Array
                (
                    [text] => Array
                        (
                            [query] => Linux shell编写端口扫描脚本
                            [operator] => or
                            [minimum_should_match] => 2<45%
                        )

                )

        )

    [highlight] => Array
        (
            [fields] => Array
                (
                    [text] => stdClass Object
                        (
                        )

                )

            [pre_tags] => #em#
            [post_tags] => #/em#
        )

    [size] => 8
    [from] => 8
)
Array
(
)
Array
(
    [query] => Array
        (
            [match] => Array
                (
                    [title] => Array
                        (
                            [query] => Linux shell编写端口扫描脚本
                        )

                )

        )

    [highlight] => Array
        (
            [fields] => Array
                (
                    [title] => stdClass Object
                        (
                        )

                )

            [pre_tags] => #em#
            [post_tags] => #/em#
        )

    [from] => 0
)
Array
(
)
Array
(
    [query] => Array
        (
            [bool] => Array
                (
                    [must] => Array
                        (
                            [0] => Array
                                (
                                    [match] => Array
                                        (
                                            [title] => Array
                                                (
                                                    [query] => Linux shell编写端口扫描脚本
                                                )

                                        )

                                )

                        )

                    [must_not] => Array
                        (
                            [0] => Array
                                (
                                    [term] => Array
                                        (
                                            [cate1] => 电子书籍
                                        )

                                )

                        )

                )

        )

    [highlight] => Array
        (
            [fields] => Array
                (
                    [title] => stdClass Object
                        (
                        )

                )

            [pre_tags] => #em#
            [post_tags] => #/em#
        )

    [size] => 5
    [from] => 0
)
Array
(
)
Linux shell编写端口扫描脚本 - 爱码网
yuzly

Linux shell编写端口扫描脚本

需求:

扫描特定主机

扫描特定主机的特定端口

扫描特定网段

扫描特定网段中哪些主机开放了特定的端口

源码如下:

#/bin/bash
#该脚本用于对特定目标主机进行常见端口扫描(加载端口字典)或者指定端口扫描,判断目标主机开放来哪些端口
#用telnet方式
#作者:雨中落叶
#博客:https://www.cnblogs.com/yuzly/
IP=$1
#获得IP的前三位
threeIP=$(echo $IP |awk -F. '{print $1"."$2"."$3"."}') 
#获得IP的第四位
endIP=$(echo $IP | awk -F. '{print $4}')
if [ $1 ]
then
  if [ $2 ]
    then
    #IP的第四位为0表明是一个网段,对整个网段进行特定端口的扫描,发现哪个主机开放特定端口
    if [ $endIP -eq 0 ]
    then
      for ((i=128;i<=140;i++))
      do
        (sleep 1;)|telnet $threeIP$i $2 2>&1 |grep "Connected to $threeIP$i">/dev/null 
        if [ $? -eq 0 ]
        then
          echo "主机$threeIP$i开放$2端口!"
        else
          echo "主机$threeIP$i的$2端口关闭!"         
        fi
      done
    else
      #IP的第四位不为0,说明是单个IP,对单个IP进行特定端口扫描
      (sleep 1;)|telnet $1 $2 2>&1 |grep "Connected to $1">/dev/null
       if [ $? -eq 0 ]
       then
         echo "主机$1开放$2端口!"
       else
         echo "主机$1的$2端口关闭!"      
       fi
    fi  
  else
      #脚本只有一个IP参数,根据IP的第四位是否为0来判断是否为网段,因为没有端口参数,扫描默认端口
    if [ $endIP -eq 0 ]
    then
      for ((i=128;i<=130;i++))
      do
        for port in $(cat defaultport.txt)
        do
          (sleep 1;)|telnet $threeIP$i $port 2>&1 |grep "Connected to $threeIP$i">/dev/null
          if [ $? -eq 0 ]
          then
            echo "主机$threeIP$i开放$port端口!"
          else
            echo "主机$threeIP$i的$port端口关闭!"          
          fi
        done
      done
    else
      #脚本只有IP一个参数,没有端口参数,加载端口字典扫描默认端口
      for i in $(cat defaultport.txt)
      do
        #与目标主机的特定端口只通信1秒,根据返回的数据来判断目标是否开放特定端口
        (sleep 1;)|telnet $1 $i 2>&1 |grep "Connected to $1">/dev/null
        if [ $? -eq 0 ]
        then
          echo "主机$1开放$i端口!"
        else
          echo "主机$1的$i端口关闭!"        
        fi
      done
    fi
  fi       
else
  echo "******************************************* 
  该脚本的用法示例:  
  $0 192.168.1.1(扫描默认端口)
  $0 192.168.1.1 portx(扫描指定IP指定端口portx)
  $0 192.168.1.0(扫描指定网段,扫描默认端口)
  $0 192.168.1.0 portx(扫描指定网段指定端口portx) 
******************************************** "  
fi

1.测试,扫描特定主机(没有给出端口,扫描默认端口文件) 

2. 测试,扫描特定主机的特定端口

3. 测试,扫描特定网段(没给端口参数,扫描默认端口文件)  #实验为例减少测试时间只扫描了很小的网段 

4.测试,扫描特定网段中哪些主机开放了特定的端口

 

相关文章: