The stupid question is the question not asked | |
PerlMonks |
Re: Net::DNS::Resolver doesn't return the desired domain nameby cavac (Parson) |
on Jun 11, 2015 at 16:30 UTC ( [id://1130060]=note: print w/replies, xml ) | Need Help?? |
First of all, after re-formatting, adding the usual use strict; use warnings; and adding the missing use Net::DNS::Resolver and adding the missing variables, i get Net::DNS::Resolver->new(): nameservers must be an arrayref. Let's fix that and add some actual nameservers and an IP to query:
This gives us the result Got: PTR -> 251.34.92.216.in-addr.arpa Took me a while playing with the code and reading the docs to find out the problem. Turns out that you have to use type-specific methods to parse the part of the answer you actually want. The complete answer from the nameserver is something like this record:
name() returns the first part, the name ("key") of that record. But what you want is the second part (the "value"), for which you need type-specific query functions, in this case ptrdname() from the Net::DNS::RR::PTR module. So, here's what i finally ended up with:
Which results in:
Be aware: Not every IP will resolve to a hostname. You will need to add appropriate error handling as well as handling Nameserver connection and lookup errors of all kinds.
"For me, programming in Perl is like my cooking. The result may not always taste nice, but it's quick, painless and it get's food on the table."
In Section
Seekers of Perl Wisdom
|
|