从文件中提取并将详细信息粘贴到另一个文件

时间:2017-11-15 00:10:58

标签: linux shell unix terminal nmap

问题在于从" nmap non-gui版本" 中提取Oracle虚拟框(第2段)的详细信息。我了解" grep" "循环" 的使用情况,并且已经尝试了所有内容,但如果连接了其他系统则会尝试同一个网络。我只是希望程序找到单词 "(Oracle VirtualBox虚拟网卡)" 并提取其上方的所有内容,直到找到空行。

**
  

root @ PopeyeTheSailorMan:〜#nmap 192.168.43.0/24> log.txt的

**

Starting Nmap 7.60 ( https://nmap.org ) at 2017-11-15 04:08 UTC
Nmap scan report for 192.168.43.1
Host is up (0.0025s latency).
Not shown: 999 closed ports
PORT   STATE SERVICE
53/tcp open  domain
MAC Address: 9A:10:94:51:42:ED (Unknown)

***Nmap scan report for 192.168.43.136
Host is up (0.00071s latency).
Not shown: 994 closed ports
PORT     STATE SERVICE
22/tcp   open  ssh
80/tcp   open  http
111/tcp  open  rpcbind
139/tcp  open  netbios-ssn
443/tcp  open  https
1024/tcp open  kdm
MAC Address: 08:00:27:D3:73:2E (Oracle VirtualBox virtual NIC)***

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

Nmap done: 256 IP addresses (3 hosts up) scanned in 16.00 seconds

2 个答案:

答案 0 :(得分:2)

如果我理解你的问题,这就是你想要的:

awk '/Oracle VirtualBox virtual NIC/' RS= Log.txt

使用您的示例输入:

$ awk '/Oracle VirtualBox virtual NIC/' RS= Log.txt
***Nmap scan report for 192.168.43.136
Host is up (0.00071s latency).
Not shown: 994 closed ports
PORT     STATE SERVICE
22/tcp   open  ssh
80/tcp   open  http
111/tcp  open  rpcbind
139/tcp  open  netbios-ssn
443/tcp  open  https
1024/tcp open  kdm
MAC Address: 08:00:27:D3:73:2E (Oracle VirtualBox virtual NIC)***

如何运作

  • RS=

    这告诉awk一次读取一个段落。

  • /Oracle VirtualBox virtual NIC/

    这告诉awk打印包含字符串Oracle VirtualBox virtual NIC的任何段落。

答案 1 :(得分:1)

    grep -n ^$ Log.txt | sed s/://

将给出空行号(适用于数组)

    grep -n 'Oracle VirtualBox virtual NIC' Log.txt | cut -f1 -d\:

将给出目标的行号

使用数组,循环和sed来理解你想要的块

    sed -n '8,19p' Log.txt