A Python script to resolve hostnames and retrieve both IPv4 and IPv6 addresses.
host-ip/
├── src/
│ └── get_host_ip.py # Main script
├── tests/
│ └── test_get_host_ip.py # Test cases
├── results/ # Output JSON files
├── hostnames.txt # Input file with hostnames
└── README.md
- Resolves hostnames to IP addresses using direct DNS queries (like nslookup)
- Supports both IPv4 (A records) and IPv6 (AAAA records)
- Returns FQDN (Fully Qualified Domain Name)
- Outputs results in JSON format
- Handles DNS lookup errors gracefully
- Falls back to socket resolution if DNS query fails
python src/get_host_ip.py hostnames.txtCreate a hostnames.txt file with one hostname per line:
google.com
localhost
github.com
Results are saved as JSON in the results/ directory:
[
{
"hostname": "google.com",
"fqdn": "google.com",
"ipv4": ["142.250.185.46"],
"ipv6": ["2607:f8b0:4004:c07::71"]
}
]Run the test suite:
python -m pytest tests/Or using unittest:
python -m unittest tests/test_get_host_ip.py -vIf IPv6 addresses are not being captured:
- Test DNS resolution directly:
python src/test_dns.py <hostname>- Compare with nslookup:
nslookup <hostname>- Ensure dnspython is installed:
pip install -r requirements.txt- Python 3.6+
- dnspython (for direct DNS queries like nslookup)
- Clone the repository:
git clone https://github.com/atulksin/host-ip.git
cd host-ip- Install dependencies:
pip install -r requirements.txtMIT