pythonpython-3.xreverse-dns

Python running DNS lookups on a list of IP addresses from .txt file


I am having a hard time figuring out how to get python to do a DNS lookup for every IP address in my text file. I am getting a TypeError: getaddrinfo() argument 1 must be string or None. Can anyone lend a hand with this?

import socket

ip_list = open('output.txt', 'r')
ais = socket.getaddrinfo(ip_list, 0)

print(ais)

Solution

  • You are passing the file iteable object directly as argument to socket.getaddrinfo when you need to iterate over the file iterator and pass the strings to the function instead. Assuming each line contains an IP:

    with open('output.txt') as f:
        for ip in f:
            out = socket.getaddrinfo(ip, 0)
            ...
    

    Notes: