试图制作IP扫描仪,输出问题

时间:2014-12-07 18:49:53

标签: bash awk

好的,所以我正在尝试制作一个IP扫描器,它只输出我网络上主机的IP地址,并打开端口。这就是我到目前为止......

< #!/bin/bash

nmap 192.168.1.1-254> scan.txt
cat scan.txt | grep "report" | cut -d " " -f 5 >

和我的输出......

FIOS_Quantum_Gateway.fios-router.home
192.168.1.100
192.168.1.103
MT1.fios-router.home
192.168.1.154

我尝试了多种剪切和awk组合,但我无法让它工作......任何想法?

2 个答案:

答案 0 :(得分:1)

假设您在名为“my_nmap_file.txt”的文件中有以下nmap输出

Starting Nmap 6.40 ( http://nmap.org ) at 2014-12-07 15:19 EST
Nmap scan report for 192.168.1.1
Host is up (0.020s latency).
Not shown: 991 closed ports
PORT      STATE SERVICE
23/tcp    open  telnet
53/tcp    open  domain
80/tcp    open  http
139/tcp   open  netbios-ssn
445/tcp   open  microsoft-ds
3333/tcp  open  dec-notes
5555/tcp  open  freeciv
49152/tcp open  unknown
49153/tcp open  unknown
MAC Address: C4:3D:C7:8F:03:19 (Netgear)

Nmap scan report for 192.168.1.2
Host is up (0.025s latency).
All 1000 scanned ports on 192.168.1.2 are closed
MAC Address: 58:C3:8B:63:52:96 (Samsung Electronics)

Nmap scan report for 192.168.1.8
Host is up (0.058s latency).
All 1000 scanned ports on 192.168.1.8 are closed
MAC Address: F0:27:65:79:1A:B7 (Murata Manufactuaring Co.)

Nmap scan report for 192.168.1.6
Host is up (0.0000070s latency).
All 1000 scanned ports on 192.168.1.6 are closed

您可以尝试使用命令行

cat 'my_nmap_file.txt'|grep "Nmap scan report for *" |grep -o  -E '[0-9.]+'|sed '1 s#.*#Your Gateway: &#'

这将匹配如下所示的以​​下行并获取其IP地址

Nmap scan report for 192.168.1.2

输出

Your Gateway: 192.168.1.1
192.168.1.2
192.168.1.8
192.168.1.6

注意:我为第一行添加了一个名为'Your Gateway:'的小句子

我注意到您还希望打开与ip地址匹配的端口。如果您可以提供nmap为开放端口提供的消息。也许我们也可以找到那种模式。

答案 1 :(得分:0)

尝试:

grep -e "report +[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\"

这将查找report后跟一个或多个空格,后跟四个空格。仅分开的数字组。有更优雅的方法可以做到这一点,但不容易看到它在做什么。