Todd Chester has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I actually do not need to know the answer to this, I am just playing around learning Perl.

The following Linux Bash command will give you back your Public WAN IP address:

dig +short myip.opendns.com @resolver1.opendns.com

I am just curious if there is a sneaky way to do the same in Perl?

Many thanks,

-T

Replies are listed 'Best First'.
Re: How would I do this "dig" command in Perl?
by Corion (Patriarch) on Nov 10, 2015 at 09:38 UTC

    This is basically a DNS query. Most likely, you can do the same by using (for example) Net::DNS, AnyEvent::DNS or by querying your WAN modem if it has UPnP enabled.

    In principle, you could get most of this by using gethostbyname, except that this will not use the resolver1.opendns.com resolver but whatever your machine is configured to use for resolving hostnames.

      The real question is how would you do it just in simple Perl!

      Imagine a server that has no nslookup or dig utilities.
      It has a recent Perl, you can "use Socket;", but not Net::DNS. You are not root, so can't install anything.
      You have an IP address for something you think is a DNS server, and want to lookup some names. How?

      Thank you!
Re: How would I do this "dig" command in Perl?
by VinsWorldcom (Prior) on Nov 10, 2015 at 15:03 UTC

    You can, but with a command line this long (admittedly could be shortened as I'm not the best golfer) - the 'dig' way just seems so much more efficient.

    VinsWorldcom@C:\Users\VinsWorldcom> perl -MNet::DNS -e "$r=Net::DNS::R +esolver->new(nameservers=>['resolver1.opendns.com']); $p=$r->send('my +ip.opendns.com'); use Socket qw( inet_ntoa ); print inet_ntoa $p->{an +swer}->[0]->{address};"
      ++VinsWorldcom; added to my oneliner list, but a little shortened
      <P> perl -MNet::DNS -MSocket -E "say inet_ntoa(Net::DNS::Resolver->new(na +meservers=>[shift])->send(shift)->{answer}->[0]->{address});" resolve +r1.opendns.com perlmonks.org<P>
      Eventually in the same file there is another quick hack about DNS query using gethostbyname:
      perl -MSocket -e "print map {qq($_ : ).inet_ntoa((gethostbyname$_)[4] +).qq(\n)} @ARGV"

      L*

      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.