0

I want to use an nmap script to convert ASN -> IP ranges

So the nmap script is used like that,

nmap --script targets-asn --script-args ragets-asn.asn=12345

This command return the IP ranges in 12345 ASN.

I have a list.txt with multiple ASN. I want to run the nmap command for all the ASNs in the list.txt file

Bash is preferred.

Thanks in advance.

JNevill
  • 46,980
  • 4
  • 38
  • 63
letok
  • 1

1 Answers1

0
#!/bin/bash

for asn in $(cat list.txt)
do
        nmap --script targets-asn --script-args targets-asn.asn=$asn 2>&1|sed -e "1,/$asn/ d"|egrep -v 'WARNING|Nmap'|awk '{print $2}'
done

Some details:

The for loop will read the list.txt file one line at a time and execute the nmap with the asn provided.

The "2>&1" tells nmap to send both stdout and stderr to the pipe (|)

The sed command cases the command to output only data after the $asn match (remove the nmap header and other info messages that are not needed)

The egrep removes some more of the messages we don't want.

The awk command prints only the IPs without the "|" "_" tree symbols

AAber
  • 1,562
  • 10
  • 14
  • Just a note, using all three `sed`, `grep`, and `awk` is really not necessary as `sed` (or `awk`) alone is powerful enough to handle final filtering. – Anubis Apr 15 '19 at 14:25