Created
April 21, 2023 02:54
-
-
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
This file contains 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
#!/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