Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Dns lookups

by perlaintdead (Scribe)
on Dec 11, 2013 at 07:52 UTC ( #1066557=CUFP: print w/replies, xml ) Need Help??

i just needed an nslookup like tool so i wrote this oneliner.


use Socket;print$_,"\n"and map$a.=ord.".",split'',inet_aton$_ and chop +$a and print$a,"\n\n"and$a=""for@ARGV;

Replies are listed 'Best First'.
Re: Dns lookups
by hippo (Canon) on Dec 11, 2013 at 17:04 UTC

    Any reason not to use gethostbyname() instead?

    perl -MSocket -e '@h=gethostbyname($_) and print "$_\n".inet_ntoa($h[4])."\n\n" for @ARGV;'

    Still no good for IPv6, obviously, but a bit less wheel reinvention.

      haha. yeah i guess i should have looked into that module a bit more. I was just using what i was familiar with; needed something quick and dirty.

        and also you don't need those parens for gethostbyname

      you can also get rid of that pesky array

      use Socket;print "$_\n".inet_ntoa((gethostbyname$_)[4])."\n\n" for @AR +GV;

        The array is there to cope with the possibility of an unresolvable host. If you try your example above with args like


        the unresolvability of would cause it to die on the first argument, whereas with the array it will just skip that and carry on to the remaining args. To my mind that's a better UX but others may disagree.

Re: Dns lookups
by VinsWorldcom (Parson) on Dec 11, 2013 at 15:49 UTC

    Nice, but doesn't do IPv6.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://1066557]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2018-06-18 04:50 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (108 votes). Check out past polls.