#! /usr/bin/env python
# coding=UTF-8
# Filename: basic-shodan.py
# Template python-shodan code from: 
# eireann.leverett@ioactive.com 
# & created for hack.lu workshop

import time
from shodan import WebAPI

SHODAN_API_KEY = "insert your API key here"

# Create a connection to the Shodan API
api = WebAPI(SHODAN_API_KEY)

#Queries Shodan for a search term and then stores results in a list of dictionaries 
def query_Shodan(term, callback):
	templist = []
	try:
		i=1
		while True:
			#Search Shodan
			results = api.search(term,page=i)

			#Construct a temp dictionary to store results
			for result in results['matches']:
				temp = {}
				temp["Query"] = term 
				host = api.host('%s' %result['ip'])
				ip = '%s' %result['ip']
				temp["IP"] = ip.encode('ascii', 'replace')
				hostnames = s = ''.join(host.get('hostnames', None))
				temp["Hostnames"] = hostnames.encode('ascii', 'replace')
				os = '%s' %host.get('os', None)			
				temp["OS"] = os.encode('ascii', 'replace')
				port = '%s' %host.get('port', None)			
				temp["Port"] = port.encode('ascii', 'replace')
				banner = '%s' %host.get('banner', None)
				temp["Banner"] = banner.encode('ascii', 'replace')
				ccode = '%s' %host.get('country_code', None)
				temp["Country Code"] = ccode.encode('ascii', 'replace')
				cname = '%s' %host.get('country_name', None)
				temp["Country Name"] = cname.encode('ascii', 'replace')
				city = '%s' %host.get('city', None)				
				temp["City"] = city.encode('ascii', 'replace')
				longitude = '%s' %host.get('longitude', None)
				temp["Longitude"] = longitude.encode('ascii', 'replace')
				latitude = '%s' %host.get('latitude', None)
				temp["Latitude"] = latitude.encode('ascii', 'replace')
				timestamp = '%s' %host.get('timestamp', None)
				temp["Timestamp"] = timestamp.encode('ascii', 'replace')
				updated = '%s' %result['updated']
				temp["Updated"] = updated.encode('ascii', 'replace')
				data = '%s' %result['data']				
				temp["Data"] = data.encode('ascii', 'replace')
				templist.append(temp)
				callback(temp)
			i += 1
	except Exception, e:
		#No results found, print no 'matches'
		print 'No %s\r' %e
	#Returns a list of dictionary objects. Each dictionary is a result
	return templist


def print_result(info):
	print info

#Example usage with inbuilt printing
list = query_Shodan('net:87.239.102.7', print_result)

#Example pretty printing
for match in list:
	for field in match:
		print field
		print match[field]
		print ''
	print '-------------------'
