Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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:

10.1.150.1,Up,1/3/2012,host.domain.lan
10.1.150.2,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 - 192.168.0.50-100 | grep Host | awk  '{print $2","$5","$3}'

Which gives me this:

192.168.0.75,Up,()
192.168.0.76,Up,(server01.domain.com)
192.168.0.77,Up,(server02.domain.com)
192.168.0.78,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.

share|improve this question
add comment

2 Answers

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".

share|improve this answer
add comment

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 - 192.168.0.50-100 | perl -lanE 'if(/initiated (.*?) as:/){$d=$1;$,=","}else{print@F[1,4],$d,$F[2]=~/\((.*)\)/ if/Status:/}'
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.