bash 如何使用 nmap 确定给定范围内的哪些 IP 具有端口 80?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3773183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 19:41:15  来源:igfitidea点击:

How to determine which IPs in a given range have port 80 using nmap?

bashshellnmap

提问by bananah

I'm new to bash scripting and I'm trying to get this working:

我是 bash 脚本的新手,我正在尝试使其正常工作:

Scanning an IP range for finding devices with the port 80 open... I think it has to look like this:

扫描 IP 范围以查找打开端口 80 的设备...我认为它必须如下所示:

#!/bin/bash
echo -----------------------------------
for ip in 192.168.0.{1,.255}; do
nmap -p80 192.168.0.1
      if #open; then
            echo "{ip} has the port 80 open"
      else
            #do nothing
fi
done
echo -----------------------------------
exit 0

I also just want to see the results like this:

我也只想看到这样的结果:

-----------------------------------
192.168.0.1 has the port 80 open
192.168.0.10 has the port 80 open
192.168.0.13 has the port 80 open
192.168.0.15 has the port 80 open
-----------------------------------

(So without errors or nmap's normal outputs..)

(所以没有错误或nmap正常输出..)

Can someone help me for this?

有人可以帮我吗?

回答by Manuel Faux

nmapcomes with a nice output parameter -oG(grepable output) which makes parsing more easy. Also it is not necessary to iterate through all IP addresses you want to scan. nmap is netmask aware.

nmap带有一个很好的输出参数-oGgrepable 输出),这使得解析更容易。此外,没有必要遍历要扫描的所有 IP 地址。nmap 可以识别网络掩码。

Your example can be written as:

你的例子可以写成:

nmap -p80 192.168.0.0/24 -oG - | grep 80/open

The -oGenables the grepable output, and -specifies the file to output to (in this case stdout). The pipe symbol redirects the output of nmap (stdout) to grep, which only returns lines containing 80/openin this case.

-oG使能的grepable输出,并且-指定文件输出到(在这种情况下stdout)。管道符号将 nmap (stdout) 的输出重定向到 grep,它只返回包含80/open在这种情况下的行。

回答by Mohamed

Try this

尝试这个

nmap --open -p80 192.168.0.*

The --openwill only list host with port 80 open. This way you save having to check in your shell script as filtering is already done by nmap itself.

--open只列出主机端口80开启。这样你就不必检查你的 shell 脚本,因为过滤已经由 nmap 本身完成了。

https://nmap.org/book/man-briefoptions.html

https://nmap.org/book/man-briefoptions.html