Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Reliable FQDN from IP

by agentorange (Sexton)
on Nov 07, 2012 at 12:09 UTC ( #1002665=perlquestion: print w/replies, xml ) Need Help??
agentorange has asked for the wisdom of the Perl Monks concerning the following question:


Using 5.8.8 and due to tight control of our software stacks I cannot make use of non-core Perl modules.

I'm looking for a reliable method of returning the FQDN from an IP but using gethostbyaddr() is not reliable as it'll query /etc/hosts first which matches short hostname and the FQDN returned maybe an alias.

I cannot use Net::Nslookup or Net::DNS and am loathed to call `nslookup` if I can make use of a nice neat Perl module that'll do what I want.

Am I resigned to a system call or can I force gethostbyaddr() to use BIND or another core module to return the FQDN?


Replies are listed 'Best First'.
Re: Reliable FQDN from IP
by Tanktalus (Canon) on Nov 07, 2012 at 14:00 UTC
    Using 5.8.8 and due to tight control of our software stacks I cannot make use of non-core Perl modules.

    Yes, even you can use CPAN. On one hand, the poor woman who has to fill out our due-diligence reports for our lawyers hates the work. On the other hand, she knows that this is why I'm so productive. So we go through the extra overhead. Before I joined the team, everyone just thought they had to rely solely on whatever was already on the system. As space_monk suggests, I've pursued a path of expanding it. That's still my suggestion here.

    Talk to your manager about this. At your current rate of pay and how long it would take to rewrite the functionality provided by one of the CPAN modules, or at least the subset you need (please take into consideration the extra testing you'll have to perform), what does that cost? And what does it cost to add an extra module (or, in my case, 100 modules)? Generally, it's cheaper to use the CPAN module. And I say that even when I have to involve the lawyers for re-distribution approval.

    Please don't take "tight control" to automatically mean "no non-core". Because most people are fairly reasonable about this type of thing - if it makes you more productive, they'll probably be in favour of finding a way to help.

      I agree with this, however some people may not be willing to put in the efforts to make this happen for their orginisation/client. The time and effort to make get this going within our clients organisation was vast, but worth it. As well as technical skills, people skills are required for dealing with very difficult people/mentalities.

Re: Reliable FQDN from IP
by marto (Archbishop) on Nov 07, 2012 at 12:50 UTC

    Net::Domain is core:

    use strict; use warnings; use Net::Domain qw( hostfqdn ); my $host = hostfqdn(); print "$host\n";

    Tested under Solaris, Perl 5.8.8

    Update: Obviously this is for the current host, sorry if this doesn't actually answer the question. I'll never post another node while waiting for a hospital appointment to start.

Re: Alternative
by space_monk (Chaplain) on Nov 07, 2012 at 13:29 UTC
    Perhaps you ought to be looking at ways round the tight control of your software stack, such as expanding @INC or PERL5LIB to include local libraries?

    I'd best clarify I'm not advocating breaching security here, but there's little difference between a local library and a legitimate part of your PERL code, and this avoids having to reinvent the wheel.

      You are all correct of course and in an ideal world that would be possible. The link describing how to make use of CPAN is interesting reading but unfortunately there are situations when using code not part of a standard distribution is not viable.

      I have coded what I need using nslookup and grep, split and substr.

      Many thanks

        You should put that into a module and upload it to CPAN so that others in your situation can benefit from it...

        - tye        

Re: Reliable FQDN from IP
by Rhandom (Curate) on Nov 08, 2012 at 15:25 UTC
    require Socket; # very much core perl my $_host = gethostbyaddr(Socket::inet_aton($ip), Socket::AF_INET()) +;
    my @a=qw(random brilliant braindead); print $a[rand(@a)];
      I'd already looked into using the following which I think I'm right in saying is fundamentally the same as your code snippet suggestion (unless of course yours forces BIND in which case perfect!):
      $hostname = gethostbyaddr( inet_aton($ip), AF_INET );
      But unfortunately gethostbyaddr by design searches the sources listed in /etc/nsswitch.conf or similar.

      For me this is undesirable as my single source of truth for the FQDN is not the client but DNS. With /etc/nsswitch.conf set to local followed by bind the query is likely to resolve from /etc/hosts first which cannot be relied upon to be accurate.
        With /etc/nsswitch.conf set to local followed by bind the query is likely to resolve from /etc/hosts first which cannot be relied upon to be accurate.

        Theoretically correct. But have a look at your /etc/hosts: In any environment with a working DNS setup, you should find no other record than localhost and perhaps localhost ::1. So, for any other IP address, DNS is queried. If you fear that someone manipulates /etc/hosts, you fear that someone gains root privileges on that machine. Why do you run code on a machine you can not trust?


        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1002665]
Approved by davies
Front-paged by Old_Gray_Bear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2018-06-24 13:56 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.