2019-11-08
sed - parsing nmap -oG output using sed
stackoverflow
Question

I have a logfile

...
Host: 111.222.121.123 (111.222.121.123.deploy.static.akamaitechnologies.com) Ports: 80/open/tcp//http//AkamaiGHost (Akamai's HTTP Acceleration|Mirror service)/, 443/open/tcp//ssl|http//AkamaiGHost (Akamai's HTTP Acceleration|Mirror service)/
Host: 1.2.3.4 ()  Ports: 80/open/tcp//http//cloudflare/, 443/open/tcp//ssl|https//cloudflare/, 2052/open/tcp//clearvisn?///, 2053/open/tcp//ssl|http//nginx/, 2082/open/tcp//infowave?///, 2083/open/tcp//ssl|http//nginx/, 2086/open/tcp//gnunet?///, 2087/open/tcp//ssl|http//nginx/, 2095/open/tcp//nbx-ser?///, 2096/open/tcp//ssl|http//nginx/, 8080/open/tcp//http-proxy//cloudflare/, 8443/open/tcp//ssl|https-alt//cloudflare/, 8880/open/tcp//cddbp-alt?///
Host: 2.3.4.5 (a104-96-1-61.deploy.static.akamaitechnologies.com)   Ports: 53/open/tcp//domain//(unknown banner: 29571.61)/
...

I need to extract and convert IPs and http ports to the following format

1.2.3.4:80,443,2083

There are just two types of port fields in the logfile

80/open/tcp//http
2083/open/tcp//ssl|http

Tried to use sed but without success. I ended up with this dysfunctional command

cat ../host_ports.txt | sed -rn 's/Host: ([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}).*?([0-9]{1,5}\/open\/tcp\/\/http|[0-9]{1,5}\/open\/tcp\/\/ssl\|http).*/\1 \2/p'
Answer
1

First handle the repeating ports, and next replace Host/Port to the desired format.

sed -r 's/(Ports:|,) ([0-9]*)[^,]*/\1\2/g;s/Host: ([^ ]*).*Ports:/\1:/' ../host_ports.txt

EDIT: First I gave all ports of a line with http somewhere, now limit the result to ports with http in its description.

sed -nr 's/Ports: /, /;
         s/, ([0-9]*)[^,]*http[^,]*/,\1/g;
         s/,[^,]*\/[^,]*//g;
         s/Host: ([^ ]*)[^,]*,/\1:/p'       ../host_ports.txt
sed - parsing nmap -oG output using sed
See more ...