Simon
Simon

Reputation: 11

Query reverse dns lookups with dnspython

I am trying to query reverse lookups in dnspython. Unfortunately, the from_address() function does not let me hand over a IP by variable. Any ideas why?

#!/usr/bin/env python

import dns.resolver,dns.reversename

with open("test", "r") as ips:
    for ip in ips:
        ip = str(ip)
        n = dns.reversename.from_address(ip)
        print str(dns.resolver.query(n,"PTR")[0])

I am new to python; would be great if somebody could help out!

Upvotes: 1

Views: 9374

Answers (1)

Tony
Tony

Reputation: 53

I really doubt you are still working on this but if you print out each IP you will realize there is a newline \n in every ip which dns.reversename.from_address() does not like:

192.168.1.1

192.168.1.2

192.168.1.3

This causes an exception:

dns.exception.SyntaxError: Text input is malformed.

You can easily change the line:

ip = str(ip)

to:

ip = str(ip).strip()

and it will strip out all of the whitespace (which there should be none in a list of well formed IP addresses leaving you with this:

192.168.1.1
192.168.1.2
192.168.1.3

If you were experiencing the same text formatting exception, and your IP addresses are well formed, this should solve your problem. Sorry I'm 2 years late, I stumbled upon this Googling the dns.reversename.from_address(). If your list of IPs is not well formatted, you could use something like ippy to filter out your bad ones.

Upvotes: 3

Related Questions