Skip to content

Instantly share code, notes, and snippets.

@albertofwb
Created April 21, 2023 02:54
Show Gist options
  • Save albertofwb/82aaa77ac511f187222c89994d3f016e to your computer and use it in GitHub Desktop.
Save albertofwb/82aaa77ac511f187222c89994d3f016e to your computer and use it in GitHub Desktop.
obtain the domain's IP and corresponding real address using dig and ipinfo.io
#!/usr/bin/env python3
import subprocess
import requests
def get_domain_locations(domain_name: str) -> dict:
# Use dig command to retrieve IP addresses of domain
result = subprocess.run(['dig', '+short', domain_name], stdout=subprocess.PIPE)
ip_addresses = result.stdout.decode('utf-8').strip().split('\n')
ip_addresses = [i for i in ip_addresses if i.count('.') == 3]
# Use ipinfo.io API to get geographical location of each IP address
location_dict = {}
for ip_address in ip_addresses:
url = f'https://ipinfo.io/{ip_address}/json'
response = requests.get(url)
location_info = response.json()
# Create a location string with city, region, and country information
# location_str = f"{location_info.get('country', '')} {location_info.get('region', '')} {location_info.get('city', '')} {location_info.get('org', '')}"
location_str = f"{location_info.get('country', '')} {location_info.get('region', '')} {location_info.get('city', '')}"
# Add IP address and location information to dictionary
location_dict[ip_address] = location_str
return location_dict
import sys
domain_name = sys.argv[1]
for ip, loc in get_domain_locations(domain_name).items():
print(f"{ip:<15} {loc}")
# obtain the domain's IP and corresponding real address using dig and ipinfo.io
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment