2

The problem is regarding extracting the details of the Oracle Virtual box(2nd paragraph) from the "nmap non-gui version". I understand the usage of "grep" and "loops" and have already tried everything but what if there are other systems connected on the same network. I just want the program to find the word "(Oracle VirtualBox virtual NIC)" and extract everything above it until it finds a blank-line.

**

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
ASK Arjun
  • 111
  • 1
  • 18

2 Answers2

2

If I understand your question, this does what you want:

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

With your sample input:

$ 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)***

How it works

  • RS=

    This tells awk to read input a paragraph at a time.

  • /Oracle VirtualBox virtual NIC/

    This tells awk to print any paragraph that contains the string Oracle VirtualBox virtual NIC.

John1024
  • 109,961
  • 14
  • 137
  • 171
1
    grep -n ^$ Log.txt | sed s/://

will give the blank line numbers (suitable for an array)

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

will give the line numbers of the target

Use arrays, loops and sed to grok the block you want

    sed -n '8,19p' Log.txt
  • Thanks a lot for the deep explanation @sos . But,what I was looking for was precisely answered by **John1024** . You can check it if you want :) – ASK Arjun Nov 15 '17 at 06:25