Search code examples
pythonshodan

Shodan - Python Syntax to use "Info" Tag


So I'm currently working on a script using Shodan in Python using import shodan. The documentation around isn't the best so id thought I'd bring my issue here.

Using the Shodan CLI there is a command known as Shodan Info which displays the number of search credits that your account has remaining.

When looking at the Shodan Documentation i can see that there is an info() tag but whenever I use it there has been an error.

Does anyone know the correct syntax for using info in a python script to display the number of credits an account has?

Errors i have received:

ipinfo = shodan.info()
print (ipinfo)

Error

Traceback (most recent call last):
File "C:/Users/XXXX/OneDrive - XXXX/Documents/XX Documents/XXXX/Working 
                                             Segments/ShodanScraper.py", line 8, in <module>
ipinfo = shodan.info()
AttributeError: module 'shodan' has no attribute 'info'

And

ipinfo = shodan.Shodan(info())
print (ipinfo)

Error

Traceback (most recent call last):
File "C:/Users/XXXX/OneDrive - XXXX/Documents/XXXXDocuments/XXXXXX/Working 
                                             Segments/ShodanScraper.py", line 8, in <module>
ipinfo = shodan.Shodan(info())
NameError: name 'info' is not defined

Solution

  • You need to instantiate the Shodan class and then you will be able to use its Shodan.info() method. Here is how it looks in the official command-line interface:

    https://github.com/achillean/shodan-python/blob/master/shodan/__main__.py#L364

    Here's a short script that prints the current account usage information:

    import pprint
    import shodan
    
    key = 'YOUR API KEY'
    api = shodan.Shodan(key)
    results = api.info()
    pprint.pprint(results)