Skip to content

Instantly share code, notes, and snippets.

@achillean
Created December 9, 2015 02:26
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save achillean/65285db312cf7614bc9c to your computer and use it in GitHub Desktop.
Save achillean/65285db312cf7614bc9c to your computer and use it in GitHub Desktop.
Read a Shodan JSON file and print out the full host information.
#!/usr/bin/env python
#
# export_hosts.py <export.json.gz>
#
import gzip
import shodan
import simplejson
import sys
# Setup the Shodan API
YOUR_API_KEY = "..."
api = shodan.Shodan(YOUR_API_KEY)
# Load the records
for line in gzip.open(sys.argv[1], 'r'):
# Decode the line into a banner
banner = simplejson.loads(line)
# Grab the full host information for an IP
if 'ipv6' in banner:
host = api.host(banner['ipv6'])
else:
host = api.host(banner['ip_str'])
# Serialize the host information into a JSON string
print(simplejson.dumps(host))
@achillean
Copy link
Author

To run the script save as "export-hosts.py" and fill in the "YOUR_API_KEY" variable with your own Shodan API key. Then run the script using:

python export-hosts.py export.json.gz

Where export.json.gz is an JSON export generated from the website or with the shodan command-line utility.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment