<?xml version="1.0" encoding="windows-1252"?>
<node id="776" title="How do I find out my hostname/domainname/IP address?" created="1999-10-07 20:32:15" updated="2005-08-15 08:04:16">
<type id="834">
perlfaq nodetype</type>
<author id="519">
faq_monk</author>
<data>
<field name="doctext">


&lt;P&gt;
The normal way to find your own hostname is to call the &lt;CODE&gt;`hostname`&lt;/CODE&gt;
program. While sometimes expedient, this has some problems, such as not
knowing whether you've got the canonical name or not. It's one of those
tradeoffs of convenience versus portability.

&lt;P&gt;
The Sys::Hostname module (part of the standard perl distribution) will give you the hostname after which you can find out the 
&lt;FONT SIZE=-1&gt;IP&lt;/FONT&gt; address (assuming you have working 
&lt;FONT SIZE=-1&gt;DNS)&lt;/FONT&gt; with a 
&lt;CODE&gt;gethostbyname()&lt;/CODE&gt; call.

&lt;P&gt;
&lt;PRE&gt;    use Socket;
    use Sys::Hostname;
    my $host = hostname();
    my $addr = inet_ntoa(scalar(gethostbyname($name)) || 'localhost');
&lt;/PRE&gt;
&lt;P&gt;
Probably the simplest way to learn your 
&lt;FONT SIZE=-1&gt;DNS&lt;/FONT&gt; domain name is to grok it out of /etc/resolv.conf,
at least under Unix. Of course, this assumes several things about your
resolv.conf configuration, including that it exists.

&lt;P&gt;
(We still need a good 
&lt;FONT SIZE=-1&gt;DNS&lt;/FONT&gt; domain name-learning method for non-Unix systems.)

&lt;P&gt;
</field>
</data>
</node>
