Re: (OT) Best way to resolve a hosts IP address
On Mon, 19 Aug 2002, Raffaele Sandrini wrote:
> Hi
> Sorry this is a simple question and i thought it would be easyer to post
> it here that extra search a adequate perl mailing list.
>
> I need a safe way to get a hosts IP Address in a Perl script. I tried it
> with the NetAddr::IP module and got fair results but on some machienes it
> returned 192.168.1.1 on hosts wich had another ip.
> All the hosts are in the same subnet so it has not to be a complicated
> thing a function like gethostip() would be cool.
If you already have the name of the system just use
gethostbyname($hostname)
If this is for the localhost and you don't know it's name yet use
the standard module Sys::Hostname. It has a function hostname() that
will return the hostname. It may not be the full name, but that
doesn't matter when you're using gethostbyname().
Oh, remember that this function returns the *raw* address, so you will
have to convert it to the familiar 4-octet string yourself. You can use
unpack or inet_ntoa() for that.
I don't know if that will fix the 192.168.* problem, though...
HTH
Grx HdV
--
J.A. de Vries aka HdV
Delft University of Technology
Computing Centre
Reply to: