Formatting nmap output

Viewed 612

I have an nmap output looking like this

Nmap scan report for 10.90.108.82
Host is up (0.16s latency).

PORT   STATE SERVICE
80/tcp open  http
|_http-title: Did not follow redirect to https://10.90.108.82/view/login.html

I would like the output to be like

10.90.108.82 http-title: Did not follow redirect to https://10.90.108.82/view/login.html

How can it be done using grep or any other means?

3 Answers

You can use the following nmap.sh script like that:

<nmap_command> | ./nmap.sh

nmap.sh:

#!/usr/bin/env sh

var="$(cat /dev/stdin)"
file=$(mktemp)
echo "$var" > "$file"

ip_address=$(head -1 "$file" | rev | cut -d ' ' -f1 | rev)
last_line=$(tail -1 "$file" | sed -E "s,^\|_, ,")

printf "%s%s\n" "$ip_address" "$last_line"
rm "$file"

If you do not mind using a programming language, check out this code snippet with Python:

import nmapthon as nm

scanner = nm.NmapScanner('10.90.108.82', ports=[80], arguments='-sS -sV --script http-title')
scanner.run()

if '10.90.108.82' in scanner.scanned_hosts(): # Check if host responded
    serv = scanner.service('10.90.108.82', 'tcp', 80)
    if serv is not None: # Check if service was identified
        print(serv['http-title'])

Do not forget to execute pip3 install nmapthon.

I am the author of the library, feel free to have a look here

Looks like you want an [nmap scan] output to be edited and displayed as you wish. Try bash scripting, code a bash script and run it.

Here's an link to a video where you might find an answer to your problem: https://youtu.be/lZAoFs75_cs

Watch the video from the Time Stamp 1:27:17 where the creator briefly describes how to cut-short an output and display it as we wish. If you require, I could code an bash script to execute an cut-shorted version of the output given by an nmap scan.

Related