Created
May 23, 2024 19:12
-
-
Save markbosky/39118aa4e1dd88573e9a7f7a39557e27 to your computer and use it in GitHub Desktop.
Python script to check DNS info given a list of domains and output to text file
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
import dns.resolver | |
import sys | |
def get_dns_info(domain): | |
try: | |
result = {} | |
for record_type in ['A', 'AAAA', 'MX', 'NS', 'TXT', 'CNAME']: | |
try: | |
answers = dns.resolver.resolve(domain, record_type) | |
result[record_type] = [str(rdata) for rdata in answers] | |
except dns.resolver.NoAnswer: | |
result[record_type] = 'No Answer' | |
except dns.resolver.NXDOMAIN: | |
result[record_type] = 'Domain does not exist' | |
break | |
except Exception as e: | |
result[record_type] = f'Error: {e}' | |
return result | |
except Exception as e: | |
return {'Error': str(e)} | |
def read_domains_from_file(file_path): | |
try: | |
with open(file_path, 'r') as file: | |
domains = file.readlines() | |
return [domain.strip() for domain in domains] | |
except Exception as e: | |
print(f"Error reading file: {e}") | |
sys.exit(1) | |
def write_output_to_file(output, file_path): | |
try: | |
with open(file_path, 'w') as file: | |
file.write(output) | |
except Exception as e: | |
print(f"Error writing to file: {e}") | |
def main(input_file, output_file): | |
domains = read_domains_from_file(input_file) | |
output = '' | |
for domain in domains: | |
dns_info = get_dns_info(domain) | |
output += f"DNS Information for {domain}:\n" | |
for record_type, records in dns_info.items(): | |
output += f" {record_type}: {records}\n" | |
output += '\n' | |
print(output) | |
write_output_to_file(output, output_file) | |
if __name__ == "__main__": | |
if len(sys.argv) != 3: | |
print("Usage: python script.py <input_file> <output_file>") | |
sys.exit(1) | |
input_file = sys.argv[1] | |
output_file = sys.argv[2] | |
main(input_file, output_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment