1

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!

Simon
  • 11
  • 1
  • 4
  • Your code works for me with hardcoded list of IPs. Are you sure the file's content is what you think it is? – DeepSpace May 24 '15 at 20:33
  • IP addresses are contained within the file overhanded. When I type one manually it works fine. However, something must be wrong with the file. Does it Need a Special Encoding? Is it recognized as text? – Simon May 24 '15 at 23:34

1 Answers1

2

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.

Tony
  • 43
  • 1
  • 5