views:

343

answers:

3

I need to write a bash script that will take a grepable nmap output file that displays IP addresses with port 80 open and copy the IPs that have port 80 open to another text file. The output looks similar to this:

# Nmap 4.76 scan initiated Thu Dec  3 13:36:29 2009 as: nmap -iL ip.txt -p  80 -r -R -PN --open -oA output
Host: 192.168.1.100 () Status: Up
Host: 192.168.1.100 () Ports: 80/open/tcp//http///
Host: 192.168.1.100 () Status: Up
# Nmap done at Thu Dec  3 13:36:29 2009 -- 3 IP addresses (3 hosts up) scanned in 0.28 seconds

I am fairly new to bash scripting so I am not sure where to start with this. If you can help me with this script it would be much appreciated.

+1  A: 

Use grep and sed/awk

grep -e '80/open/tcp' infile | awk '{print $2}' | sort -u > outfile

would be my first attempt.

retracile
You don't need to redirect the file into `grep` - it will take the filename as an argument.
Dennis Williamson
@Dennis - I plead "brain glitch". Fixed, thanks.
retracile
+1  A: 

not being familiar with nmap invocation and output format, but still, this should work:

nmap | grep -e 'Ports:.80\/' |sed 's/Host:.//;s/.(.*//'|sort -u > out
catwalk
You should be able to combine those two invocations of `sed` into one (perhaps with multiple `-e` clauses).
Dennis Williamson
@Dennis Williamson: you are correct, two seds is too much
catwalk
+1  A: 

this can be reduced to an awk call:

awk '/80\/open/{print $2}' infile > iplist_port_80
kon