Hello, everyone.
And welcome to python with nmap.
So here we are going to write a very cool program in
which we are going to run a port
scanner which will be made by us. So
we are going to write a python Port scanner
using nmap. And then we are going to run a
port scan on to a public network and
our localhost Network.
Cool, so I have taken up sublime.
Text editor you can use any IDE that you
want.
So I have written first import and map for importing
the nmap library.
Then I have written begin equals to
75 and end equals to 80.
So these are the range of ports to be
scanned.
I want to scan the first Port that
should start from 75 and then 80 so I
have made two variables which we are going to assign two
values. There are 75 and 80.
Perfect.
next
Target equals to Raw input enter your
target. So we are assigning the target IP
address over here that need to be scanned through a
variable called as Target.
Here we have assigned a new variable and we
are going to take a raw input from the user and going to
utilize that particular input which will be obviously
IP address to be scanned.
next
we are going to instantiate a port
scanner object as you can see over here, which is
nmap dot Port scanner.
perfect next
Here we are going to run a for Loop.
So I have taken a for Loop for iteration. So I'm
using a variable that is I range
method which accepts two parameters, which
is begin and end.
So the first one is begin second one is end, but the
value is not included. So I
want to scan the port 80 as well. If I do
not add plus 1 it will not scan Port 80
and it will only
Scan till 79. So I want to include
that particular Port as well. So and plus 1
into this particular for Loop.
Now we have included our Port 80 as well.
Let's move forward and here you can
see result equals to scanner.scan wherein
Target comma string of
I so what we are doing actually here. So we
are going to scan the Target Port.
So we have made a scan function which requires
two parameter.
First is the IP to be scanned. Okay?
the next one is
Str which is basically nothing but we
are typecasting I into a string format.
next
Is a result equals to this and
the result will be containing several information
as our output in Json, but from that
we only need limited output. So basically
we have to just check if the port is open
or closed. So we will access only that required information. So
we are going to write it this way wherein
we are going to get scan in our output
and
The TCP as well as the state out of
which we only want to show the results which are
accessing two values.
The first one is I that is the port number as you
can see over here. So Port will give the port
number which starts from 75 to
80 as we saw in the beginning.
This is the begin variable and this is the end variable.
The next one is state.
Which is basically the result and
we will get the port status either. It is
open closed or filtered.
Yeah, so now we have understood the code. It is quite pretty
simple. Now. Let us quickly jump back over here
and let's try to execute over particular Python
program and run our Port scanner.
So I have saved this ads PS sportscana.py. Let
me just run it and it will ask me enter
your target.
If you remember if you go back to the code.
This is the raw input which is asking us
to enter our Target. So let
us quickly type.
our loopback address
of this machine and hit enter.
As you can see it has started scanning from Port 75 to
Port 80 and all the
ports are closed for now as there are
no open ports into this machine. So let us quickly open
one port wherein I'm going to use Python Library
as well. So I'm
going to use the python module which is a simple HTTP server.
So this particular module is used
to create a
Simple HTTP server using python so
I have used the port 80.
So as you can see, the server has successfully started on
Port 80 now, let us scan and see
if our Port scanner
detects that if Port 80 is open or not. So
I will just hit enter.
Again, let me type my loopback IP address.
And let's wait for the scan.
As you can see it has scanned for 75 76 77
78 79 and we
are waiting for 80 and we have got the result as
open.
Perfect. So we have got the result as open because Port
80 was scanned and over
here. You can see we have got a request from
nmap.
As you can see over here.
And and the particular request was to check that if
the port is open or not. So nmap tries
to send a particular request as you
can see over here.
Multiple requests are being sent to identify the port
status and end map as identify that that
particular port is open and communicating with
nmap and hence given that result
in the output as open.
So this was one of the very simplest way to make your own
port scanner in Python.
So I hope you guys understood.
Thank you.