仅使用perl核心模块列出计算机中的所有IP地址

时间:2017-02-24 13:21:23

标签: perl-module

如何仅使用perl核心模块列出计算机中的所有IP地址。

我使用的代码如下。但它只列出一个IP地址而不是一台机器的所有IP地址。

Can i be able to get list of ip addresses from a machine using perl.

我只想要ipv4地址。     要获取所有IP地址,我应该使用ifconfig命令,或者有一种方法perl可以通过它的API给我这个。

#!/usr/bin/perl -w
use Socket;


sub getIpAddressbyHost {

my ($hostname,$refAddress) = @_;

return -1 unless ($hostname ne "");

 my $address = "";  
  unless ($hostname =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {

    eval {
      if($hostname ne "") {
        my $nformat = inet_aton($hostname);
        unless(defined($nformat)) {
          print("Error at getIpAddressbyHost for hostname $hostname\n");
          return -1;
        }
       $address = inet_ntoa(inet_aton($hostname));
       $$refAddress = $address;
     }
   };
  if ($@) {
   print("Error at getIpAddressbyHost for host $hostname : $@");
    #$self->addErrorDetails("Error at getIpAddressbyHost for host $hostname : $@");
    return -1;
  }
}
else {
  $$refAddress = $hostname;
}

return 1;

}


 my $ipaddr = "Can't find";
 print "enter the hostname: \n";
 my $input = <STDIN>;
 chomp($input);

 my $retval = getIpAddressbyHost($input,\$ipaddr);

 if($retval == 1){

  print("the value of ipaddr: $ipaddr \n");

 }

0 个答案:

没有答案