ultimately I am looking to insert some info into a database. So I'm trying to output a file from something (probably nmap) that is formatted like this:,Up,1/3/2012,host.domain.lan,Down,1,3/2012,host2.domain.lan

I do not HAVE to get the date from the nmap scan but it would be helpful. I can add it when I insert the data into the DB.

I'm pretty sure that nmap can do this along with some grep, awk, and sed foo, but those things aren't my strongest points.

EDIT: I'm open to suggestions about output formats.. XML? I need to run this daily (give or take) and import the results to MySQL

EDIT2: I've gotten this far;

nmap -sn -oG - | grep Host | awk  '{print $2","$5","$3}'

Which gives me this:,Up,(),Up,(server01.domain.com),Up,(server02.domain.com),Up,(server03.domain.com)

Next; Can I remove the parens? add the date? Nmap always says what time it starts and finishes but it doesn't include that on each line... so I can't grab it w/ the awk statement.

2 Answers 2


Have a look in the Nmap book's chapter 13, "Output formats", it has a section on "Grepable Output" and even one on "Output to a Database".


If you want another program (database, etc.) to use Nmap output, always use the XML output (-oX). Depending on your preferred programming languages, there will often be a parser library already written: Nmap::Parser for Perl, Nmap::Parser for Ruby, ndiff (comes with Nmap) for Python. If you really need to use command pipelines, there are a few XML utilities that could be made to work (XmlStarlet seems to be fairly popular).

Depending on your needs, you could make use of an existing project that supports Nmap XML import. Metasploit, Dradis, and OpenVAS are a few very different tools that support importing from Nmap and storing in a database.

Even if you insist on using the "Grepable" output (which is deprecated), I would highly recommend using a real scripting/programming language (Perl, Python, Ruby, etc.) to do the parsing, especially for things like putting the date on every line or stripping parentheses. Command pipelines involving Nmap usually end up as monstrosities like this:

nmap -sn -oG - | perl -lanE 'if(/initiated (.*?) as:/){$d=$1;$,=","}else{print@F[1,4],$d,$F[2]=~/\((.*)\)/ if/Status:/}'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .