In this demonstration,
we're going to walk through a simple bash script,
break down each line to understand what it's doing,
and then modify the script to enhance its functionality.
So first, what is the purpose of this script?
Well, if we look through it,
we can see some common terms, right?
So we see IP, we see ping, et cetera.
So let's break this down.
First, starting at the top,
we have a dead giveaway of the purpose of the script,
'cause typically a lot of coders,
they're going to put in the actual purpose at the top
and comment it out.
And then we have this usage here.
So when we actually run the script
in our bash shell, we're going to use
./ping_sweep.sh,
and then we're going to input a network.
Okay, so we see we have three octets.
So we have the first, second, and third octet.
So we're not going to put the entire network here,
we're only going to put the first three octets
and it's going to finish the rest.
Now here, we're going to focus on the if statement,
this whole if function.
The if statement checks if the user
provided a network prefix
as the argument were running the script.
So, in bash, this $1,
what that means is the first argument
after you run the command, right?
So, here, for this example usage,
we have ./ping_sweep.sh
192.168.
This here is $1.
Okay, this is the first argument.
So after a space,
that's when bash start counting the argument.
So this will be the first one.
Put a space here, that will be the second one,
space, third, et cetera.
So it's going to check and see if it has anything there.
And if not, it's going to run this echo command.
Next, this network line here,
this line assigns the value of the first argument passed
to the script, to the variable network.
So 192.168.1 will pass to the
variable called network.
Now moving down with the
echo starting point sweep,
okay, so pretty much it's just going
to print out a statement letting the user know
that we've started the ping sweep,
but we're only going to focus on 0/24 networks.
So that's going to be the CIDR notation for that.
Alright, so what's happening here is that
for each number from 1 to 254,
it assigns that number to the variable IP.
Every time it does this,
the script inserts the IP value
into the function that follows.
Now this loop will systematically check each IP address
within the specific range.
So all these numbers,
every time it goes from 1, going to go to IP2,
going to go IP, but every time it does that,
it's going to input it into this function. Okay?
So here we have ping -c 1 -w 1
$network.$ip command.
Okay, now the -c 1 option sends just one ping,
while -w 1 sets the timeout of 1 second for that ping.
The $network.$ ip constructs
a full IP address
by combining the network prefix
with the current IP value from our loop.
And the final part, the >/dev/null,
et cetera,
discards all output, both standard and error.
So nothing is printed on the console
unless we specifically want to see it.
Alright, and then this portion here,
we have another if statement.
And so this dollar sign question mark,
it just holds the exit status of the last command executed.
Then we have the equal sign 0 checks the status.
Now this is more so kind of like cleanup things
and more so kind of like maintenance thing.
So don't really too much concern yourself with that one.
And then this echo, just going to let us
know if the host is up
and it's going to print out the host
and the IP address that's up.
Again, just checking to make sure that it actually
gets a ping from it.
And if it's not up, then we're going to skip it.
Alright, so then once you finish this,
we're going to have ping sweep completed.
Alright, so let's just check this out really quick.
So let's minimize this, open this.
Now, let's just pull up the script really quick.
So I'm going to ./ping_sweep.
And remember I'm going to press Enter with no argument.
So there's nothing being input here.
And so when this dollar sign 1 is equal to nothing,
then we should print out this.
So let's try that out, alright, perfect.
So there was no argument input 'cause we didn't put anything
after the ping_.sweep.sh.
And so now we have this echo command.
So it's giving us information of how to use the actual tool.
So next thing we're going to do is actually use an argument.
So I'm going to use 45.33.32.
Now I'm not completing the network,
so I only want the first three octets of the network
and it's going to finish it with the .0/24.
It's going to append that to the network variable,
'cause again, once I input this, it's going to assign it
to the network variable,
and then the network variable will be appended with .0/24.
I'm going to press in there.
Okay, we see it's working,
we see it's actually scanning the network from 1 to 254
as indicated here,
and it's just going through each one to check
and see if the host is up.
And it's going to continue to do this all the way to 254.
Now we don't need to continue to run this,
but we see that it's up and working.
So I'm just going to exit this out right now, the screen.
Okay, perfect, now next we're going to modify the code
to focus on ports as well.
So we're no longer just going to check
to see if the host is up.
We're going to see which ports are currently running
on there as well.
Okay, we're going to make this very simple.
We're going to go back to the text editor,
so subline text,
and we're simply going to insert an nmap command.
So nmap automatically checks for pings
and check to see if it hosts up.
But again, we're just trying to understand
how to modify code,
how a script works, and to see the output,
So we're going to change this.
We're just going to simply put: nmap -p.
So we are going to scan it for ports 22, 80,
and 443,
using the network,
and $ip.
Okay, I'm going to save this.
Again, this is a very basic change to the code,
earlier, we're only pinging to see if the host was up.
Now we're going to perform a nmap scan against the host
for ports 22, 80, and 443.
Now, let's see what happens now.
Okay, let's minimize this. Let's run the command again.
All right, perfect, so we see that
it's performing the nmap scan
as planned.
Again, a script is nothing more than a set of instructions
bundled into one file.
So, we're going to go ahead and stop this here.
So, to sum things up,
we started with a simple bash script,
designed to ping a /24 subnet
to check and see if the hosts were up.
We then modified the script to include a nmap scan,
which was inserted here,
and it's going to check for ports 22, 80, and 443.
Remember, it's important to understand
what these lines in the script are for.
So for example, understanding that the $1
refers to the first argument of the script,
understanding how a for statement looks,
it requires the done, here,
understanding an if statement, which ends here.
And also understanding that this statement here,
sequence 1 254, means that we're going to iterate
the IP addresses from 1 all the way to 254.
Please take your time to brush up on these syntax rules
when it comes to bash scripts.
I hope you enjoy this demonstration
and I'll see you in the next one.