[comp.lang.perl] DNS lookup within perl

sherk@umd5.umd.edu (Erik Sherk) (08/03/90)

Hello,

	I need to find the name of a host from within a perl script. The
following seems to work, but there must be a better way! 

----------------

($1, $2, $3, $4) = split(/\./, $IPaddr);
$query = sprintf("set querytype=ptr\n%d.%d.%d.%d.in-addr.arpa", $4,$3,$2,$1);
$nsreply = `echo '$query' | nslookup 2>/dev/null | \
					egrep 'in-addr'|egrep -v 'domain'`;
($foo, $foo, $foo, $foo, $host) = split( /[ \t\n]+/, $nsreply );

----------------

	(I split the third line for readability) 

	You will note that this takes four processes to do what one
system call should do! Also, there is the need to redirect stderr
from nslookup.

	Is there any way to "split" a string without specifing the
field separators?

	Please help, this is my first perl script!

Erik Sherk
sherk@nmc.cit.cornell.edu

P.S. please email direct, as I am 85 articles behind in comp.lang.perl

schwartz@groucho.cs.psu.edu (Scott Schwartz) (08/03/90)

In article <7028@umd5.umd.edu> sherk@umd5.umd.edu (Erik Sherk) writes:

	   I need to find the name of a host from within a perl script. The
   following seems to work, but there must be a better way! 


Try this:

#!/usr/bin/perl

@inaddr = (130, 203, 2, 4);

if (($name) = gethostbyaddr(pack("C4", @inaddr), &AF_INET)) {
  print $name, "\n";
}
else {
  print "Unknown address\n";
}

sub AF_INET {2;}