Using Shodan’s API: A Simple Authentication Example

We’ve been looking at simple API examples that don’t require any authentication. Today let’s take a look at a simple example that does require some authentication, and for that we’ll use the very popular API of Shodan.

If we look at the Python code below, there’s a variable that you would need to enter for your own individual Shodan API key. When the Python code runs, it asks for an IP address to look up. The user would then type in whatever IP address they want information on.

The code then takes that IP address and goes out to visit Shodan using the API. It prints information that there’s really only one of on the system – for instance, one organization, one country, one city, one operating system, etc. Then for things that there could be multiples of, like listening ports, we see that it prints “open ports” and then walks through all the results from ports and prints them using the f-string for printing and pretty printing. If there’s any errors, it should say “error” and then print it out.

Like I said, the examples we’ve been using so far have been unauthenticated. Here is an authenticated example, and tomorrow we’ll take a look at how to make this even easier than we currently are when we’re using Python to query an API.