I want to retrieve information using the Python Shodan API. 1. Search mongodb in shodan…
- User
I want to retrieve information using the Python Shodan API. 1. Search mongodb in shodan 2. Output search results by IP and country 3. Count statistics by version just show me example code
- Assistant
Sure, here's an example code that demonstrates how to use the Python Shodan API to achieve what you're looking for:
pythonimport shodan # API key for accessing Shodan API SHODAN_API_KEY = "YOUR_SHODAN_API_KEY_HERE" # create a Shodan API client api = shodan.Shodan(SHODAN_API_KEY) # search for MongoDB instances using the Shodan API query = "mongodb" results = api.search(query) # output the search results by IP and country for result in results["matches"]: ip = result["ip_str"] country = result["location"]["country_name"] print(f"{ip} ({country})") # count statistics by version version_stats = {} for result in results["matches"]: version = result.get("mongodb", {}).get("version") if version in version_stats: version_stats[version] += 1 else: version_stats[version] = 1 # output the version statistics for version, count in version_stats.items(): print(f"{version}: {count}")