cvs commit: perlfaq perlfaq9.pod
[email protected] (brian d foy) 10 Nov 2005 00:35:31 -0000
| Newsgroups | perl.cvs.perlfaq |
|---|---|
| Message-ID | <[email protected]> |
cvsuser 05/11/09 16:35:31
Modified: . perlfaq9.pod
Log:
* How do I find out my hostname/domainname/IP address?
+ I updated the answer to include Net::Domain
Revision Changes Path
1.25 +29 -21 perlfaq/perlfaq9.pod
Index: perlfaq9.pod
===================================================================
RCS file: /cvs/public/perlfaq/perlfaq9.pod,v
retrieving revision 1.24
retrieving revision 1.25
diff -u -r1.24 -r1.25
--- perlfaq9.pod 13 Oct 2005 19:43:13 -0000 1.24
+++ perlfaq9.pod 10 Nov 2005 00:35:31 -0000 1.25
@@ -596,29 +596,37 @@
$msg[$msgno] .= $_;
END { print @msg[ sort { $sub[$a] cmp $sub[$b] || $a <=> $b } (0 .. $#msg) ] }
-=head2 How do I find out my hostname/domainname/IP address?
-
-The normal way to find your own hostname is to call the C<`hostname`>
-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.
-
-The Sys::Hostname module (part of the standard perl distribution) will
-give you the hostname after which you can find out the IP address
-(assuming you have working DNS) with a gethostbyname() call.
+=head2 How do I find out my hostname, domainname, or IP address?
+X<hostname, domainname, IP address, host, domain, hostfqdn, inet_ntoa,
+gethostbyname, Socket, Net::Domain, Sys::Hostname>
+
+(contributed by brian d foy)
+
+The Net::Domain module, which is part of the standard distribution starting
+in perl5.7.3, can get you the fully qualified domain name (FQDN), the host
+name, or the domain name.
+
+ use Net::Domain qw(hostname hostfqdn hostdomain);
+
+ my $host = hostfqdn();
+
+The C<Sys::Hostname> module, included in the standard distribution since
+perl5.6, can also get the hostname.
+
+ use Sys::Hostname;
+
+ $host = hostname();
+
+To get the IP address, you can use the C<gethostbyname> built-in function
+to turn the name into a number. To turn that number into the dotted octet
+form (a.b.c.d) that most people expect, use the C<inet_ntoa> function
+from the <Socket> module, which also comes with perl.
use Socket;
- use Sys::Hostname;
- my $host = hostname();
- my $addr = inet_ntoa(scalar gethostbyname($host || 'localhost'));
-
-Probably the simplest way to learn your DNS 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.
-
-(We still need a good DNS domain name-learning method for non-Unix
-systems.)
+
+ my $address = inet_ntoa(
+ scalar gethostbyname( $host || 'localhost' )
+ );
=head2 How do I fetch a news article or the active newsgroups?