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

杀马特。学长 韩版系。学妹 提交于 2019-12-25 04:12:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!