In this lesson, I'm going to walk you through
a short Bash simple script,
and we're going to figure out what it's doing.
Now on exam day, they're not going to give you something
that says bin/bash like I have here on the top.
Now that makes it really easy for me to identify
that this is a Bash script.
But on test day, they're going to leave that out.
Let's look at the rest of this and see what it's doing
and figure out, is this a Bash script
or would it be something else?
Well, the first command we have is if
$1 does not equal empty.
What is that telling us?
It's saying that if there is nothing there,
then don't do anything.
But if it's not empty, then our target file
is going to equal $1, that variable.
We're going to set our report directory to var/log/nmap.
So we're probably going to be doing something
with Nmap if we're setting it to the Nmap directory.
Next, for target in $cat target file.
Now there we can see that for the variable target,
we are going to assign each line inside that target file
to the value of target, and then run through that loop.
Once we run out of lines, that would be the end of
that four loop and we will be finished
with doing these commands.
Next we're going to echo scanning target,
whatever that target is
that we just pulled in from that target file.
So what do we have on the next line?
Well, we have /user/bin/nmap -oN,
and then a lot of stuff.
As you can see, it takes up over two lines.
Now what is this telling us to do?
Well, this is saying we're going to run the Nmap command
and we're going to use it with these different parameters
and we'll go into that in a second.
You should remember these from our Nmap scanning lectures.
And then we have the done.
Otherwise we're going to echo you did not provide any
command line options, usage program, parentheses, file,
echo, echo, and then FI to end the if loop.
So what is this short Bash script really doing for us?
Have you figured it out yet?
Well, if not, pause the video,
look through it, and then restart it
so you can see if you have the right answer.
All right, you're back? Great.
So what is this script doing?
Well, it's going to take in from the command line,
we're going to run it by writing the program name,
the script, and then giving it a file.
Every time there is something in that file,
like an IP address or a domain name,
it's going to run an Nmap scan against it.
So all this command is doing is allowing me
to take whatever inputs from a file, one per line
for IP addresses and run a different scan on them each time.
So in this case, I'm going to pull the file in.
I'm going to read one at a time from there,
and every time there's something on the line
and it's not empty, I'm going to run an Nmap scan
against it.
Now what is this Nmap scan doing?
Well this is a slow comprehensive scan.
We're using T4, which is not too aggressive.
We're using -A, which searches for everything,
including OS detection and versioning.
We're doing SS for a syn scan.
We're using -oN for output files in Nmap format,
and each one is going to be saved
to its own file named target.nmap.
So if I had three IP addresses, say I had scanme.nmap.org,
jasondion.com, and diontraining.com,
it's going to pull each of those.
It's going to do a full Nmap scan
and save a file, Jasondion.com.nmap,
diontraining.com.nmap, right?
And I can go back and look at those results later
so I can run this script, go home, come back the next day
and have all my scans completed.
It's really great.
Now for bonus, you may want to look
through this Nmap scan and figure out
what are all these parameters like the SS for syn scan,
the T4 for the timing, the -A for all versioning,
V for verbose, right?
What are all those things? And you can look through that.
And so instead of me having to run this scan
three different times on three different IPs,
I can just give it a file
and the script will pull the information out
and run those scans for me.
So what might a question like this look like on the exam?
Well, they might give you something like this
and then say what's the function of it?
And the correct answer would be
that this is performing an Nmap scan
against targets provided from an input file.
Something like that, right? That's the idea here.
So you don't need to understand every single nuance
of this script, but you should be able to identify
that it's a bash script and what the basic function is.
In this case, it's going to go through a file,
read each time what's in that file,
and run an Nmap scan against them.