Running nmap via bash script

Hello fellow scripters...

I am trying to set up a script for work that reads the contents of a file of IP address and network names, takes these and runs a given nmap scan against them and then saves the output to a file with the filename of the network name.

Such as...

IP file looks like this:
1.2.3.0/24 network1
2.3.4.0/24 network2

Script would cat the contents of this file, take the IP portion and put it in the nmap scan command and then the output of that nmap scan would be saved to a file called network1.nmap, network2.nmap, etc.

Is this possible? If so, how? It seems like I need to do *something* with the IP file so that the network address and name are saved/referenced separately I just can't figure out what I need to do to make this happen.

Thank you for any assistance and guidance you can offer!

------ Post updated at 05:06 PM ------

I am slightly new to scripting and volunteered to see if I could work this out to give myself some experience in both scripting and online research.

Everything is "possible".

When you post here, you should post your coding attempt and work, as this is a technical support forum and we prefer people do post their work and what they have tried to do.

What have you tried? What code have you attempted so far?

Here's my script:

!/bin/bash
NETWORKS='�at networks.txt'
for NETWORK in $NETWORKS
     nmap -sn -oN /root/Desktop/$NETWORK
done
echo "Finished!"

The networks.txt file has my list of network addresses I want to scan. If I enter a single IP address, like 1.2.3.4, the script works fine and the outpu file is named 1.2.3.4.nmap. But when I enter a network address, like 1.2.3.0/24, script errors off saying it cannot open file /root/Desktop/1.2.3.0/24 for writing. This error tells me the script is at least SEEING the right information in the txt file it's just not processing it properly. If I put single ' marks around $NETWORK so that it reads /root/Desktop/'$NETWORK' the script runs and the output file name is $NETWORK and not 1.2.3.0/24.

Is there a way to associate each network address by a number instead of a name and reference that number? Like:

#1   1.2.3.0/24
#2   2.3.4.0/24

So I would code the script to take the network address and save it in a file name by its associated number (#1.nmap, #2.nmap, etc.)

Thanks!

It's always good to search the forums before posting a question (it's also a core forum rule... ).

For example, see:

... seems to have all the ingredients to help you solve your problem.

Thank you! That additional thread helped, along with a little extra tweaking. I've got it working now. I appreciate the assist!