-->

DEVOPSZONES

  • Recent blogs

    A Python script that reads a list of fully qualified domain names (FQDNs) from a file, looks up their A records and writes the results to another file

    Python script 

    Here is a Python script that reads a list of fully qualified domain names (FQDNs) from a file, looks up their A records using the socket library, and writes the results to another file: 

    #!/usr/bin/python3
    import socket

    # Open the input and output files
    with open('input.txt', 'r') as input_file, open('output.txt', 'w') as output_file:

    # Read each line (FQDN) from the input file
    for line in input_file:

    # Strip whitespace and newline characters from the line
    fqdn = line.strip()

    try:
    # Look up the A record for the FQDN
    ip_address = socket.gethostbyname(fqdn)

    # Write the FQDN and its A record to the output file
    output_file.write(fqdn + ',' + ip_address + '\n')

    except socket.gaierror as e:
    # If the FQDN cannot be resolved, write an error message to the output file
    output_file.write(fqdn + ',' + str(e) + '\n')

    Code can be found at get-A-record.
    In this example, the script assumes that the input file is called "input.txt" and the output file is called "output.txt". The script reads each line (FQDN) from the input file, strips any whitespace or newline characters, and attempts to look up the A record for the FQDN using socket.gethostbyname(). If the lookup is successful, the script writes the FQDN and its A record to the output file in the format "FQDN,IP_ADDRESS". If the lookup fails, the script writes an error message to the output file in the format "FQDN,ERROR_MESSAGE".




     

    No comments