0

I tried this in linux machines

my $a =  $ENV{HOSTNAME};
print "\nhostname = $a\n";

i get this,

hostname = sims5.eng.netapp.com

i tried same in Solaris, but i get nothing.

hostname = 

I can use below code,

use Sys::Hostname;
$host = hostname;

but is there any other way of getting hostname without importing Sys::Hostname in solaris machines.

Thanks.

alanc
  • 4,102
  • 21
  • 24
user2380503
  • 21
  • 2
  • 3
  • 4
    what's the aversion to `use Sys::Hostname;` ? Why do you not want to do it that way? – Ahmed Masud May 21 '13 at 06:00
  • 1
    It's not neccessary to install `Sys::Hostname` directly from CPAN, if you want your script to work _only_ on Solaris. Check its [source code](http://cpansearch.perl.org/src/RJBS/perl-5.18.0/ext/Sys-Hostname/Hostname.pm) (it's rather tiny) and choose the method that works on your system. Also, you can just copy the module as is: its only dependency is `Carp`, and it's probably installed in your system. – raina77ow May 21 '13 at 06:08
  • no reasons for not using sys::hostname, but my doubt is why ENV{HOSTNAME} is not working in solaries ? – user2380503 May 21 '13 at 06:12
  • 2
    Because the corresponding environment variable is not set, obviously. – raina77ow May 21 '13 at 06:13
  • 1
    Based on a quick survey of the servers here, it appears that setting HOSTNAME is specific to Red Hat-derived Linux distributions. Not only Solaris, but also Debian and Ubuntu do not set HOSTNAME by default. – Dave Sherohman May 21 '13 at 09:20

2 Answers2

3

If you reeeally don't want to use modules, you can just read the hostname from the following file: /etc/nodename

Dmitry Mina
  • 3,842
  • 1
  • 14
  • 10
1

This is as @raina77ow suggested using source from Sys::Hostname,

my $host = hostname() or warn "No hostname";

sub hostname {
  require "sys/syscall.ph";
  require "sys/systeminfo.ph";
  my $host = "\0" x 65; ## preload scalar
  syscall(&SYS_systeminfo, &SI_HOSTNAME, $host, 65) != -1 or return;

  $host =~ tr|\0\r\n||d;
  return $host;
}
mpapec
  • 50,217
  • 8
  • 67
  • 127