Skip to content
Advertisement

How to include comments from Nmap script in the output file

There might be a better way to do this, but what I have is an input file called “list.txt” of IP addresses for my Nmap scan like so:

192.168.1.1   #Router
192.168.1.120 #PC
192.168.1.52  #Phone

Then I scan with Nmap using and output to a file using:

nmap -iL list.txt >> output.txt

Additionally I have used sed to make the “output.txt” look like this:

Host: 192.168.1.1       Status: Up
Host: 192.168.1.120     Status: Down
Host: 192.168.1.52      Status: Up

I would like to include the comments from the input “list.txt” file, but cannot find a way this can be done. The ideal “output.txt” would look like this:

Host: 192.168.1.1       Status: Up    #Router
Host: 192.168.1.120     Status: Down  #PC
Host: 192.168.1.52      Status: Up    #Phone

Is there a way to include the comments in the “list.txt” to be in the “output.txt”?

Advertisement

Answer

Another option is to run awk with the original file and the new output.txt file:

awk 'NR==FNR { ip[$1]=$2;next } { printf "%st%sn",$0,ip[$2] }' list.txt output.txt > finaloutput.txt

Process the first (NR==FNR) and create an array called IP with the IP address (first space delimited field) as the key and the comment the value. Then when processing the second file (output.txt), print the line, plus the comment for the IP taken from the ip array.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement