0

So! I have an nmap scan that saves the output to a file, nmapscan.txt:

sudo nmap -sP 192.168.1* > nmapscan.txt 

I also have a few other commands to clean up and save all desired output to one file, livehosts.txt:

cat ./temp/nmapscan.txt | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' > ./temp/macaddresses.txt 

Which saves the mac addresses. And:

grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' ./temp/nmapscan.txt > ./temp/ipaddresses.txt 

Which saves the IP addresses. And:

cat ./temp/nmapscan.txt | grep -o -P '(?<=for).*(?=.attlocal)' > ./temp/hostnames1.txt 

Which saves the hostnames. And:

cut -d' ' -f 2 ./temp/hostnames1.txt > ./temp/hostnames2.txt 

Which I did to clean up the hostnames (sorry, forgot to include this in original post)... And:

cut -d' ' -f 1 ./temp/hostnames2.txt | paste ./temp/macaddresses.txt ./temp/ipaddresses.txt - > ./temp/livehosts.txt 

.. which is the end product that combines the desired data and displays like:

xx:xx:xx:xx:xx:xx 192.168.1.1 Netgear xx:xx:xx:xx:xx:xx 192.168.1.111 John's Toilet Cam 

I want to do the scan and write, again, and want the livehosts.txt file to be more or less, updated. Simply doing the scan and write again, as is, writes over the file. And not all devices are connected at different times.

I've tried:

>> 

But that just doubles up the data already in the file.

Would doubling up the data, cutting recurring data, then writing to a new file be the best way to go about it?

How would you go about doing this? Thanks!

3
  • Is nmapscan.txt the same file as ./temp/nmapscan.txt - or are you perhaps re-processing the same file over again? Commented Mar 13, 2020 at 2:15
  • Yeah, it's the same file. Sorry! Commented Mar 13, 2020 at 2:27
  • The easiest way is probably to double up the data, and afterwards use sort -u. Commented Mar 13, 2020 at 8:10

1 Answer 1

1

There are too many manipulations that are unnecessary.

First of all, use the -oG option (grepable format). This will make your job easier. Source: Nmap Reference Guide - Output. And there is no need to do file redirection (> nmapscan.txt) since nmap already has an -oN option. You can log to a file but still have output to the console.

I am not sure your target specification is even legal:

nmap -sP 192.168.1* 

Maybe nmap managed to interpret it correctly, AFAIK it's not valid syntax.

Instead, use 192.168.1.0/24 or 192.168.1.1-255 (assuming you want scan a small network with addresses 192.168.1.0 through 192.168.1.255.

See: Nmap Reference Guide - Target Specification

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.