假设我正在执行 nmap shell 来找出一个简单的输出。我们都熟悉这个:
Starting Nmap 6.47 ( http://nmap.org ) at....
Nmap scan report for www.google.com (172.217.26.68)
Host is up (0.085s latency).
rDNS record for 172.217.26.68: sin10s02-in-f68.1e100.net
Not shown: 998 filtered ports
PORT STATE SERVICE
80/tcp open http
443/tcp open https
Nmap done: 1 IP address (1 host up) scanned in 9.38 seconds
现在我想要具体的端口状态和服务例如:
Open ports are:
80
443
或类似的东西。重点是我想使用特定的输出并在其他地方使用它。任何人都可以给我一个非常非常简单的例子吗?
答案1
尝试使用以下代码:
echo "Open ports are: ";nmap $HOST |awk -F'/' '/open/ {print $1}'
答案2
$ echo 'Open ports are:';nmap localhost -oG -|grep -oP '[0-9]*(?=/open)'
Open ports are:
22
6000
答案3
your_nmap_cmd |
sed -ne '
/^PORT/!d
h;n # grab PORT line & prepare for port info
\|/|!q # we are done once no / found so quitting
s|/.* open .*||p # display port num only for open state
G;D # without reading next input goto top
'