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 {local $SIG{'__DIE__'};
43 my($test) = gethostbyname('me')}; # returns 'me' on most systems
44 if ($@) { return $host = $ENV{'SYS$NODE'}; }
46 # method 3 - has someone else done the job already? It's common for the
47 # TCP/IP stack to advertise the hostname via a logical name. (Are
48 # there any other logicals which TCP/IP stacks use for the host name?)
49 $host = $ENV{'ARPANET_HOST_NAME'} || $ENV{'INTERNET_HOST_NAME'} ||
50 $ENV{'MULTINET_HOST_NAME'} || $ENV{'UCX$INET_HOST'} ||
51 $ENV{'TCPWARE_DOMAINNAME'} || $ENV{'NEWS_ADDRESS'};
52 return $host if $host;
54 # method 4 - does hostname happen to work?
55 my($rslt) = `hostname`;
56 if ($rslt !~ /IVVERB/) { ($host) = $rslt =~ /^(\S+)/; }
57 return $host if $host;
61 Carp::croak "Cannot get host name of local machine";
64 elsif ($^O eq 'MSWin32') {
65 ($host) = gethostbyname('localhost');
66 chomp($host = `hostname 2> NUL`) unless defined $host;
71 # method 2 - syscall is preferred since it avoids tainting problems
73 local $SIG{'__DIE__'};
78 $host = "\0" x 65; ## preload scalar
79 syscall(&main::SYS_gethostname, $host, 65) == 0;
82 # method 3 - trusty old hostname command
84 local $SIG{'__DIE__'};
85 $host = `(hostname) 2>/dev/null`; # bsdish
88 # method 4 - sysV uname command (may truncate)
90 local $SIG{'__DIE__'};
91 $host = `uname -n 2>/dev/null`; ## sysVish
94 # method 5 - Apollo pre-SR10
96 local $SIG{'__DIE__'};
97 ($host,$a,$b,$c,$d)=split(/[:\. ]/,`/com/host`,6);
101 || Carp::croak "Cannot get host name of local machine";
104 $host =~ tr/\0\r\n//d;