Create a python script that open the file given in argument and process each line. Each…
- User
Create a python script that open the file given in argument and process each line. Each line contains a subdomains which should be resolved to an IP address. Output every IP address to the resolved.txt file
- Assistant
Sure! Here's a Python script that takes a filename as a command-line argument, resolves the subdomains to IP addresses and outputs them to a file called "resolved.txt":
import sys import socket # Check if filename is provided as argument if len(sys.argv) < 2: print("Please provide a filename as an argument") sys.exit(1) filename = sys.argv[1] output_file = "resolved.txt" try: with open(filename) as file: # Open the input file lines = file.readlines() with open(output_file, "w") as output: # Process each line for line in lines: # Remove leading/trailing whitespaces and newlines subdomain = line.strip() try: # Resolve the subdomain to IP address ip_address = socket.gethostbyname(subdomain) # Write the IP address to the output file output.write(ip_address + "\n") print(f"{subdomain} resolved to {ip_address}") except socket.gaierror: print(f"Unable to resolve {subdomain}") print(f"IP addresses resolved successfully and saved to {output_file}") except OSError: print(f"Unable to open file {filename}")