user2380503
user2380503

Reputation: 21

How to get hostname in Solaris machines, through perl script?

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.

Upvotes: 0

Views: 4048

Answers (2)

Dmitry Mina
Dmitry Mina

Reputation: 3842

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

Upvotes: 3

mpapec
mpapec

Reputation: 50637

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;
}

Upvotes: 1

Related Questions