Created
September 9, 2023 15:28
-
-
Save yosignals/51d19db8a9a1232e6dc5ce0052a372ce to your computer and use it in GitHub Desktop.
feed IPs to DeHashed API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import csv | |
api_endpoint = 'https://api.dehashed.com/search?query={}' | |
credentials = ('[email protected]', 'APIKEY') | |
headers = {'Accept': 'application/json'} | |
# Step 1: Read the IP addresses from a text file | |
with open('ip_addresses.txt', 'r') as file: | |
ip_addresses = [line.strip() for line in file] | |
# Step 4: Prepare a CSV file to store the results | |
with open('results.csv', 'w', newline='') as csvfile: | |
fieldnames = ['IP Address', 'Result'] | |
writer = csv.DictWriter(csvfile, fieldnames=fieldnames) | |
writer.writeheader() | |
# Step 2: Loop through each IP address and make a request to the Dehashed API | |
for ip_address in ip_addresses: | |
response = requests.get(api_endpoint.format(ip_address), auth=credentials, headers=headers) | |
# Step 3: Parse the JSON response and write data to the CSV file | |
if response.status_code == 200: | |
data = response.json() | |
writer.writerow({'IP Address': ip_address, 'Result': data}) | |
else: | |
writer.writerow({'IP Address': ip_address, 'Result': 'Failed to retrieve data'}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment