21

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

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

#!/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..)

Can someone help me for this?

4

2 回答 2

44

nmap带有一个很好的输出参数-oGgrepable output),它使解析更容易。此外,没有必要遍历您要扫描的所有 IP 地址。nmap 是网络掩码感知的。

你的例子可以写成:

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

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

于 2010-09-22T20:17:09.847 回答
17

尝试这个

nmap --open -p80 192.168.0.*

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

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

于 2015-08-17T15:49:07.047 回答