Python code to get the ip of a set of domains and subdomains from a file and save the results in another file.
It is a small way to automate the process and save everything in one place, you can choose the name of the files according to the parameter you send. Just run it and that's it. I hope you find it useful.
Parameters: python script-ips.py domains.txt ips_results.txt
import socket
import sys
# Check if an argument was provided for the domain file.
if len(sys.argv) < 2:
print("Please provide the domain file name as the first argument.").
sys.exit(1)
# Gets the domain filename from the arguments.
domain_file = sys.argv[1]
# Check if an argument was provided for the answer file.
if len(sys.argv) < 3:
print("No file name was provided for the responses. The default 'ips.txt' will be used.")
file_ips = "ips.txt"
else:
file_ips = sys.argv[2]
with open(domain_file, "r") as f_domains, open(file_ips, "w") as f_ips:
domains = f_domains.read().splitlines().
for domain in domains:
try:
ip = socket.gethostbyname(domain).
f_ips.write(f "IP address of {domain}: {ip}")
except socket.gaierror:
f_ips.write(f "Could not get IP for: {domain}")
print(f "IP address query completed. Results have been saved in '{ips_file}'.")
It is a small way to automate the process and save everything in one place, you can choose the name of the files according to the parameter you send. Just run it and that's it. I hope you find it useful.
Parameters: python script-ips.py domains.txt ips_results.txt
- code.py: name of the file with which you will store the code
- domains.txt: name of the file containing the domains and/or subdomains
- ips_results.txt: name of the file you want your results to be saved under
import socket
import sys
# Check if an argument was provided for the domain file.
if len(sys.argv) < 2:
print("Please provide the domain file name as the first argument.").
sys.exit(1)
# Gets the domain filename from the arguments.
domain_file = sys.argv[1]
# Check if an argument was provided for the answer file.
if len(sys.argv) < 3:
print("No file name was provided for the responses. The default 'ips.txt' will be used.")
file_ips = "ips.txt"
else:
file_ips = sys.argv[2]
with open(domain_file, "r") as f_domains, open(file_ips, "w") as f_ips:
domains = f_domains.read().splitlines().
for domain in domains:
try:
ip = socket.gethostbyname(domain).
f_ips.write(f "IP address of {domain}: {ip}")
except socket.gaierror:
f_ips.write(f "Could not get IP for: {domain}")
print(f "IP address query completed. Results have been saved in '{ips_file}'.")