[Autogenerated] for this section.
We will be moving back into our demonstration environment
and showing how to use each one of these different modules
starting with the IP API module.
We then extend on this and show how we can use the received
information and perform additional look apps specifically,
including information about the covered autonomous system.
We then show how we can take the location information that was
collected and plot this onto a real map.
Using google maps,
let's move into our demonstration environment and in this case we're
going to take a look and see how the IP API module can be used to obtain
some location information based on a hostname or IP address look up as I
noted in the intro to the demo.
We have also extended upon this in order to show how we can use
the returned autonomous system number to further look up
information on the company that's responsible for that IP range
as well as using the google maps, API to map the location that we have retrieved.
So what you'll see here is that we have a number of different modules
that we have imported at the beginning of the script.
The primary one that we're going to be initially using is
this IP API module which is the main module that's released
to query the IP API engine.
You also have the ability to not use this module specifically,
but you can use the request module and send it as part of like
a typical web look up the rest of these.
We have used in a number of different ways throughout the
script and we will show them as we get there.
So the first part that we're going to take a look at is this
little piece of code that we have here.
The name that we're going to look up initially is the doves dot
yahoo dot com hostname And what this first little section of code
does is it does a look up if required of the information that's
inside that look up variable.
Now, as far as this script is concerned, that look up variable can be an I.
P address or a host name and if it is a hostname that's
going to automatically perform a look up.
So if we take a look at here,
were initially going to try and see if we can convert
whatever is inside that variable into an I.
P address and if that's possible, it will put that into the IP variable.
Now, if that is not an I.
P address,
like in this case it's actually a hostname it's going to raise an exception.
The value error exception.
And because of that we know that it's not an I.
P address and at that point we're going to assume that it's a
hostname when we're going to perform a hostname look up.
So to do this get host by name method that's part of the socket module.
Now, this last part,
this last exception is if there's some unknown exception that is
raised and that is to complete out our little try loop.
So next we moved down here a little bit,
we're going to see that we have another try that we start and this is
actually where we use the IP API module that we imported and in this case
we take the IP address variable that we had retrieved from that last
little section of code that we reviewed and we run the location method in
this case it's giving it an I.
P.
Address and then it's expecting to get a result of a number of different fields
back and the example that we have shown below this shows an example of the
dictionary that is not like an example of a dictionary that is normally
retrieved should everything correctly succeed.
So if we move on and move down a little bit more again
that is wrapped in an exception.
Again,
just in case something happens that we did not expect and if everything
goes alright then it's going to move on to this else.
At that point we have retrieved the location result variable from the IP
API module and at this point we don't do anything else.
All we do is simply print the location results.
So if we show this running,
this is going to return a rather ugly looking version of the
dictionary that we have returned from that module and if you want to
take a look at that obviously you can parse it to figure out that the
IP address and the network and the different feels you can read
through it it's just sort of ugly an alternative to that is why we
imported DPP print module.
If we run this exact same query but printed out using the Pretty print module,
we can see that it's formatted in such a way that it's
considerably easier to read if we're just looking for it
to be a quick display for us.
Now with this information we can do other lookups in order to provide
additional information and that's what we're going to display next.
Now as we saw on that output, one of the things that is output on that is this A.
S.
N.
Number and there are ways that we can take that information and
perform a look up and as noted in the intro,
we can do this and and one of the places we can do a quick look up on
the web is through a company called Shadow Server.
So if we want to see what this display looks like at this point,
we're just going to not display the location result.
But we are going to use the IP variable that we created at
the initial part of the code that we showed perform a normal
look up using the request module,
which is why we put the request module at the beginning of this
and specifically the get method of that module.
So if we run this and we also run this through the P.
P.
Print command.
So it's printed out in a prettier way.
So in this case the I.
P address that has returned for the dubs dot yahoo dot com and the
authoritative company that's responsible for that specific range.
In this case the 74.6 dot 2 31.0 slash 24 range.
And the company that's listed for that A S.
N.
Is holdings.
Now the other thing we can do with this information and the focus of the
title of this module was how do we take this information and use it to map
out the geographic location of this IP address Now keep in mind that the
address that is listed for an I.
P.
Address does not necessarily equate to exactly where that device is sitting.
It equates to where it's registered to.
So if I run a server on the internet and I have an I.
P.
Address that I get from my provider,
likely if I perform a location look up on that IP address it's going
to come back with the address of that provider.
So with that stated, there are some limitations.
So if we uncommon out this last piece, the first piece,
we just print out the longitude and latitude.
We could uncommon those we run,
we'll see that it displays the latitude and longitude that is
linked with that specific IP address Now,
if we were to take this information since most people don't know
what the longitude and latitude are off top of their head and put
it into a query into the google maps,
API And in this case we are going to use the web browser module
that we imported at the beginning in order to launch whatever the
default browser is on this platform.
In this case it's crumb and show what the result is from
the google map based on these results.
So in this case it's returning that this IP address is
located or at least registered in Omaha Aw.
And so with that we will conclude this demonstration and move into our summary.