6 @EXPORT = qw(hostname);
10 Sys::Hostname - Try every conceivable way to get hostname
19 Attempts several methods of getting the system hostname and
20 then caches the result. It tries C<syscall(SYS_gethostname)>,
21 C<`hostname`>, C<`uname -n`>, and the file F</com/host>.
22 If all that fails it C<croak>s.
24 All nulls, returns, and newlines are removed from the result.
28 David Sundstrom E<lt>F<sunds@asictest.sc.ti.com>E<gt>
36 # method 1 - we already know it
37 return $host if defined $host;
41 # method 2 - no sockets ==> return DECnet node name
42 eval {my($test) = gethostbyname('me')}; # returns 'me' on most systems
43 if ($@) { return $host = $ENV{'SYS$NODE'}; }
45 # method 3 - has someone else done the job already? It's common for the
46 # TCP/IP stack to advertise the hostname via a logical name. (Are
47 # there any other logicals which TCP/IP stacks use for the host name?)
48 $host = $ENV{'ARPANET_HOST_NAME'} || $ENV{'INTERNET_HOST_NAME'} ||
49 $ENV{'MULTINET_HOST_NAME'} || $ENV{'UCX$INET_HOST'} ||
50 $ENV{'TCPWARE_DOMAINNAME'} || $ENV{'NEWS_ADDRESS'};
51 return $host if $host;
53 # method 4 - does hostname happen to work?
54 my($rslt) = `hostname`;
55 if ($rslt !~ /IVVERB/) { ($host) = $rslt =~ /^(\S+)/; }
56 return $host if $host;
60 Carp::croak "Cannot get host name of local machine";
63 elsif ($^O eq 'MSWin32') {
64 ($host) = gethostbyname('localhost');
65 chomp($host = `hostname 2> NUL`) unless defined $host;
70 # method 2 - syscall is preferred since it avoids tainting problems
76 $host = "\0" x 65; ## preload scalar
77 syscall(&main::SYS_gethostname, $host, 65) == 0;
80 # method 3 - trusty old hostname command
82 $host = `(hostname) 2>/dev/null`; # bsdish
85 # method 4 - sysV uname command (may truncate)
87 $host = `uname -n 2>/dev/null`; ## sysVish
90 # method 5 - Apollo pre-SR10
92 ($host,$a,$b,$c,$d)=split(/[:\. ]/,`/com/host`,6);
96 || Carp::croak "Cannot get host name of local machine";
99 $host =~ tr/\0\r\n//d;