问题
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.
回答1:
If you reeeally don't want to use modules, you can just read the hostname from the following file:
/etc/nodename
回答2:
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;
}
来源:https://stackoverflow.com/questions/16663115/how-to-get-hostname-in-solaris-machines-through-perl-script