内循环不会中断 [英] Inner loop won't break

查看:75
本文介绍了内循环不会中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码可以正常工作,但无法继续运行陷入内部循环的循环,不确定问题出在哪里,或者端口扫描可能会花费很多时间

code works but won't continue to run the loop stuck in the inner loop not sure where the problem is, or maybe port scan just takes alot of time

#!/bin/bash

up()
{
  ping -c 1 $1 > /dev/null
  [ $? -eq 0 ] && echo IP: $i is up.
}

for i in 192.168.0.{1..255} 
do
up $i & disown
    for port in {1..100};do
    2>/dev/null echo > /dev/tcp/$i/$port
    if [ $? == 0 ]
            then
                    {
                    echo "port $port is open"
                    continue
                    }
    fi
    done
done
exit

推荐答案

在bash中扫描Ping + TCP

我试图将脚本限制为8182个分叉,以保持礼貌.

此脚本扫描/etc/services ,然后可以扫描所有(所需)端口.(请参阅评论).

This script scan /etc/services, then could scan all (desired) ports. (see comments).

对我的复杂专用子网的完整扫描需要5分钟以上的时间(扫描255个主机上的303个端口,共发现27个)!

Full scan of my complex private subnet take more than 5 minutes to complete (scanning 303 ports on 255 hosts, found 27 up)!

#!/bin/bash
BaseIP=${1:-192.168.1}
ports=(21 22 25 80 443 9100)
# ports=({1..100})              # Uncomment this for ports 1-100

while IFS=$' \t\r\n/' read serv port prot comm ;do
 [ "$prot" = "tcp" ] && printf -v $prot[$port] %s "$serv"
done </etc/services
# ports=(${!tcp[@]})            # Uncomment this for all known ports

isup() { ping -W 1 -c1 -n $1 &>/dev/null && printf "IP: %-17sis up.\n" $1;}

tstport() { local _tst _prot=${3:-tcp}; local -n _var=$_prot[$2]
    {
        exec {_tst}<>/dev/$_prot/$1/$2 && exec {_tst}<&- &&
            printf "IP: %-16s port %6d open (%s)\n" $1 $2 ${_var:-unassigned}
    } 2>/dev/null
}
step=$((8180/(${#ports[@]}+1)))
for ((i=1;i < 255;i+=step)) {
        max=$((i+step>255?255:i+step))
        for ((l=i;l<max;l++)) {
                isup $BaseIP.$l &
                exec {dummy}< <(:)
                for port in ${ports[@]} ;do
                    exec {dummy2}< <(:)
                    tstport $BaseIP.$l $port & read -u $dummy2 -t .02
                    exec {dummy2}<&-
                done &
                read -u $dummy -t .02
                exec {dummy}<&-
            } |
                sed -une /./p
    }

排序的ouptut可能像这样:

Sorted ouptut could look like:

IP: 192.168.1.1      is up.
IP: 192.168.1.3      is up.
IP: 192.168.1.3      port     22 open (ssh)
IP: 192.168.1.15     is up.
IP: 192.168.1.15     port     22 open (ssh)
IP: 192.168.1.15     port    139 open (netbios-ssn)
IP: 192.168.1.15     port    445 open (microsoft-ds)
IP: 192.168.1.15     port    515 open (printer)
IP: 192.168.1.39     is up.
IP: 192.168.1.39     port     22 open (ssh)

这篇关于内循环不会中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆