hostname

jplindstrom on 2002-05-22T22:01:47

Did you know that there is a hostname.exe on wk2?

That's very useful, knowing there's a cross platform (for limited values of platform) command for identifying the machine.

From now on all my alert e-mails should state where they originate.

Quiz: is there perhaps a module providing the equivalent of `hostname`?


yes

gav on 2002-05-22T22:06:14

Sys::Hostname should do the trick (and it is core):
Attempts several methods of getting the system hostname and then caches the result. It tries the first available of the C library's gethostname(), `$Config{aphostname}`, uname(2), syscall(SYS_gethostname), `hostname`, `uname -n`, and the file /com/host

Re:yes

jand on 2002-05-23T03:00:03

Unfortunately it doesn't include the domain name on Windows:

perl -MSys::Hostname -e"print hostname"
foo

POSIX::uname works better:

perl -MPOSIX -e"print ((uname)[1])"
foo.activestate.com

Re:yes

gav on 2002-05-23T12:18:40

Is it worth patching Sys::Hostname to a less broken behaviour then?

Re:yes

jand on 2002-05-23T18:34:30

I would think so. The fix would be to try uname before gethostname, at least on Windows. Currently, gethostname succeeds, returning the domainless hostname and uname is never given a chance to show what it knows. :)

Re:yes

gav on 2002-05-23T20:31:00

Patch sent to author. I also patched Sys::Hostname::Long which is completely broken on win32.

Re:yes

gav on 2002-05-23T20:33:43

Gah. Email address of the author of Sys::Hostname isn't valid. Not sure what to do, so here is the fix anyway:

--- Hostname-old.pm Thu May 23 16:02:04 2002
+++ Hostname.pm Thu May 23 16:09:41 2002
@@ -51,7 +51,12 @@
 
      }
      elsif ($^O eq 'MSWin32') {
- ($host) = gethostbyname('localhost');
+ eval {
+ local $SIG{__DIE__};
+ require POSIX;
+ $host = (POSIX::uname())[1];
+ };
+ ($host) = gethostbyname('localhost') unless defined $host;
          chomp($host = `hostname 2> NUL`) unless defined $host;
          return $host;
      }

 

Re:yes

jand on 2002-05-23T22:38:01

No, it has to be fixed in the XS code for ghname(). All the Perl code is only a fallback if the XS code doesn't find a hostname.

Sys::Hostname is a core module, so patches should go to p5p.