pythonwindowstldnslookup

How to get IP address using nslookup and python on Windows


Over the years, there have been some changes in how some functions and commands provide an output. Which is why, it is difficult to follow older tutorials, which sometimes do not conform with the latest revisions to software and its commands.

One such change happened whilst I was using nslookup and python to lookup ip addresses on windows, as I do not primarily own a mac or linux.

How, can we fetch, only the IP address of a 'Top Level Url' (tld) using python and nslookup, as of 2019?


Solution

  • Do not use external tools for needs that can be done completely inside your programming language using the proper library, which can be dnspython in your case.

    In [2]: import dns
    
    In [3]: import dns.resolver
    
    In [5]: import dns.rdataclass
    
    In [7]: import dns.rdatatype
    
    In [9]: ans = dns.resolver.query('www.example.com', rdtype=dns.rdatatype.A, rdclass=dns.rdataclass.IN)
    
    In [10]: print ans.rrset
    www.example.com. 43193 IN A 93.184.216.34
    
    In [12]: print ans.rrset[0]
    93.184.216.34
    

    Read the full featured documentation at http://www.dnspython.org/docs/1.16.0/ for more details, especially input and output parameters.

    Two important points: