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

标签 bash shell nmap

我是 bash 脚本的新手,我正在努力让它工作:

正在扫描 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

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

-----------------------------------
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
-----------------------------------

(因此没有错误或 nmap 的正常输出..)

有人可以帮我吗?

最佳答案

nmap 带有一个很好的输出参数 -oG(grepable 输出),这使得解析更容易。也没有必要遍历要扫描的所有 IP 地址。 nmap 是网络掩码感知的。

你的例子可以写成:

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

-oG 启用grepable 输出- 指定要输出到的文件(在本例中为stdout)。管道符号将 nmap (stdout) 的输出重定向到 grep,在这种情况下它只返回包含 80/open 的行。

关于bash - 如何使用 nmap 确定给定范围内的哪些 IP 具有端口 80?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3773183/

相关文章:

如果 child 退出非零,Bash 的等待命令不会返回 0

linux - 如何使用 ed 在最后一次模式匹配后添加文本

linux - 通过 bash 和 ssh 逐字传递引号和其他特殊字符

c# - 从 Mono C# 运行 Bash 命令

linux - linux 命令 uptime 的输出与命令 nmap 的输出不同

linux - 带和不带 -p 标志的 Nmap,如何选择端口?

python - 无法使用 python-nmap,OSError : [Errno] 2

bash - 在 bash 中捕获 inotifywait 的输出

linux - 为什么这个shell脚本不运行?

linux - 在 bash 脚本中,如何判断脚本输出是否重定向到文件?