brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: nmap -oG writes one line per host that grep can search

Nmap
~/tips/tip-nmap-grepable-output-og

nmap -p 22,80,443 -oG scan.gnmap 192.0.2.0/24

Saves scan results as one line per host, so grep and cut can pick out open ports.

Sometimes I only want a list of hosts that have one port open, not a whole report. The grepable output format was made for that job.

nmap -p 22,80,443 -oG scan.gnmap 192.0.2.0/24
grep "22/open/tcp" scan.gnmap | cut -d' ' -f2

With -oG Nmap writes comment lines that start with # and one line per host. The fields on that line are separated by tab characters, and each one is a name, a colon, a space and the content. The docs show a line like this one:

Host: 64.13.134.52 (scanme.nmap.org)    Ports: 22/open/tcp//ssh//OpenSSH 4.3 (protocol 2.0)/, 25/closed/tcp//smtp///,

Splitting on spaces, the second field is the IP address, which is why the cut above prints a list of addresses. The Host and Status fields come first, then Ports. A field called Ignored State says which non-open state was left out of the list and how many ports had it.

Every entry in Ports has seven subfields separated by slashes: port number, state, protocol, owner, service, SunRPC info and version info. The owner subfield is always empty now, because the ident scan was removed.

The caveat is in the manual itself: this format is deprecated, and it calls XML the more powerful one. For a quick grep it is still handy. For anything you plan to keep parsing, I’d use -oX instead. The --append-output option exists for adding to an existing file, but the manual warns it does not work well for XML data.

Further reading: Nmap reference guide: grepable output, Nmap reference guide: output

More Nmap tips nmap --packet-trace -p 22,80 192.0.2.10 nmap -sV 192.0.2.10 sudo nmap -sU -p 53,67,123,161 192.0.2.10 nmap -sL 192.0.2.0/28 nmap -oX before.xml 192.0.2.10 All Nmap tips (12) All topics

Link