Reputation: 35
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"?
Upvotes: 0
Views: 254
Reputation: 34514
One idea using join
:
$ join -1 1 -2 2 -o2.1,2.2,2.3,2.4,1.2 list.txt output.txt
Host: 192.168.1.1 Status: Up #Router
Host: 192.168.1.120 Status: Down #PC
Host: 192.168.1.52 Status: Up #Phone
To reformat into a 'pretty' -t
able:
$ join -1 1 -2 2 -o2.1,2.2,2.3,2.4,1.2 list.txt output.txt | column -t
Host: 192.168.1.1 Status: Up #Router
Host: 192.168.1.120 Status: Down #PC
Host: 192.168.1.52 Status: Up #Phone
NOTE: this solution assumes both files are already sorted by IP; otherwise the files need to be pre-sorted, eg:
join -1 1 -2 2 -o2.1,2.2,2.3,2.4,1.2 <(sort list.txt) <(sort -k2,2 output.txt) [ | column -t ]
Upvotes: 0
Reputation: 12877
Another option is to run awk with the original file and the new output.txt file:
awk 'NR==FNR { ip[$1]=$2;next } { printf "%s\t%s\n",$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.
Upvotes: 1
Reputation: 781096
Read the file in a shell loop so you can get the comment and write it to the output file.
while read -r ip comment; do
result=$(nmap "$ip")
printf '%36s %s\n', "$result" "$comment"
done < list.txt > output.txt
Upvotes: 0