Alex
Alex

Reputation: 303

query with different name server

I am going to change the default name server of a domain and then get its A records! I tried to use the following code but the problem is that when I use the IP address as name server every thing works but when I used name server name such as "ns1.google.com" I could not run the code and got exception. Any thought?(I used the code from here: Set specific DNS server using dns.resolver (pythondns))

def NsLookupSpecificNS(domain):
    my_resolver = dns.resolver.Resolver()

    # 8.8.8.8 is Google's public DNS server
    my_resolver.nameservers = ['216.239.38.10']

    answer = my_resolver.query('google.com', 'A')
    try:
        for rdata in answer:
            print rdata
    except dns.resolver.NoAnswer:
            pass

I have tired the following:

def NsLookupSpecificNS(domain):
    my_resolver = dns.resolver.Resolver()

    # 8.8.8.8 is Google's public DNS server
    my_resolver.nameservers = ['ns1.google.com']

    answer = my_resolver.query('google.com', 'A')
    try:
        for rdata in answer:
            print rdata
    except dns.resolver.NoAnswer:
            pass

And here is what I got:

Traceback (most recent call last):
  File "C:\Users\My Documents\LiClipse Workspace\DNS\Lookup.py", line 49, in <module>
    NsLookupSpecificNS('google.com')
  File "C:\Users\My Documents\LiClipse Workspace\DNS\Lookup.py", line 33, in NsLookupSpecificNS
    answer = my_resolver.query('google.com', 'A')
  File "C:\Python27\lib\site-packages\dns\resolver.py", line 962, in query
    source_port=source_port)
  File "C:\Python27\lib\site-packages\dns\query.py", line 242, in udp
    if _addresses_equal(af, from_address, destination) or \
  File "C:\Python27\lib\site-packages\dns\query.py", line 169, in _addresses_equal
    n2 = dns.inet.inet_pton(af, a2[0])
  File "C:\Python27\lib\site-packages\dns\inet.py", line 51, in inet_pton
    return dns.ipv4.inet_aton(text)
  File "C:\Python27\lib\site-packages\dns\ipv4.py", line 48, in inet_aton
    raise dns.exception.SyntaxError
dns.exception.SyntaxError: Text input is malformed.

Upvotes: 1

Views: 3158

Answers (1)

denis phillips
denis phillips

Reputation: 12760

In dnspython, the nameservers instance variable takes a list of ip addresses and not domain names and that's why you're getting that error. You'll need to separately query for the address of ns1.google.com and use that address (or addresses) for the subsequent query. Of course, that begs the question of where you make that first query. For that, you can try using the system resolver.

Upvotes: 5

Related Questions