2017-03-08 16 views
0

最终的目标是使用模块创建ARP侦听器和发送者。在Perl中使用Sys:Addrr

我试图替代:

my $IP = "xx.x.x.xx"; #this is an actual local ip address 
my $MacAddress = "ff:ff:ff:ff:ff:ff"; # this is an actual macadd 
my $LAN = "xx.x.x.xx/24"; #this has the ip and subnetmask 

与模块Sys::Addrr自动获取IP和LAN每次程序运行时,这包括在不同的机器。

我得到的代码到这么多的使用cpan- https://metacpan.org/pod/Sys::HostAddr

my $sysaddr = Sys::HostAddr->new(); 
my $href = $sysaddr->ip(); 
foreach my $interface (keys %{$href}){ 
    foreach my $aref (@{$href->{$interface}}){ 
      print " $aref->{address}\n"; 
      print " $aref->{netmask}\n"; 
    } 
} 

,但我不理解这个代码或结构的sytanx足以能够拉仅使用第一个数组引用。使用Data::Dumper我能够看到分解结果,注意到它将我想要的地址分配给1的位置,但我似乎仍然无法提取它在程序中的使用。

我还注意到,MacAddress不能通过Sys::Addr获得 - 我假设需要另一个模块。

编辑: 运行时,它拉的信息是:

$VAR1 = { 
      'lo' => [ 
        { 
         'address' => 'x.x.x.x', 
         'netmask' => 'x.x.x.x' 
        } 
        ], 
      'eth0' => [ 
         { 
         'netmask' => 'x.x.x.x', 
         'address' => 'x.x.x.x' 
         } 
        ] 
     }; 

我想提取两个变量的“eth0的”内容在程序中使用。

没有这个新增加了使用的参考完整的代码如下:

use strict; 
use warnings; 

use Net::Pcap::Easy; 
use Net::Netmask; 
use Net::ARP; 
use Sys::HostAddr 

my $IP = "x.x.x.x"; #IP Address omitted for example 
my $MacAddress = "ff:ff:ff:ff:ff:ff"; #Mac Address omitted for example 
my $LAN = "x.x.x.x/24"; # address omitted for example 


my $parentpid = $$; 
my $childpid = fork() // die "Fork Failed $!\n"; 

if ($childpid){ 
    print "perl lab6.pl $IP\n"; 
    print "Scanning $LAN\n"; 
    print "Starting parent $$ with child $childpid\n"; 
    print "Starting child pid $childpid\n"; 
    &send_loop; 
    sleep 1; 
    print "Killing child $childpid\n"; 
    kill 'KILL', $childpid; 
    waitpid ($childpid, 0); 
    print "Exiting parent $$\n"; 
    exit; 
} 
else { 
    &cap_packs; 
} 

sub cap_packs{ 
    my $listener = Net::Pcap::Easy->new(
     dev    => "eth0", 
     filter   => "arp", 
     packets_per_loop => 1, 
     arp_callback  => sub { 
         my($npe,$ether,$arp,$header)[email protected]_; 
         print "Hello"; 
         if($arp->{opcode}==2){ 
          my $iphex = $arp->{spa}; 
          my @ip =($iphex=~/(..)(..)(..)(..)/); 
          my $ipstr = hex($ip[0]).".". hex($ip[1]).".". 
          hex($ip[2]).".".hex($ip[3]); 
          print "Host $ipstr is alive."; 
          } 
         } 
      ); 
    $listener->loop while 1; 
} 


sub send_loop{ 
    my $netobj = new Net::Netmask($LAN); 
    for my $Remote_IP ($netobj->enumerate) { 
     Net::ARP::send_packet(
      "eth0", 
      $IP, 
      $Remote_IP, 
      $MacAddress, 
      "ff:ff:ff:ff:ff:ff", 
      "request"); 
     } 
} 
+0

是否要访问'$ href - > {$ interface} [0]'? –

+0

编辑问题以包含所需输出。 –

+0

试试这个:'my $ netmask = $ href - > {$ interface} [0] {eth0} [0] {netmask}' –

回答

0

对于Sys::Addr我想出如何使其正常工作。

my $sysaddr = Sys::HostAddr->new(); 

my $IP; 
my $LAN; 

my $href = $sysaddr->ip(); 
ints:foreach my $interface (keys %{$href}){ 
    foreach my $aref (@{$href->{$interface}}){ 
    $IP = "$aref->{address}\n"; 
     $LAN = "$aref->{address}:$aref->{netmask}\n"; 
    if ($interface eq "eth0"){last ints} 
    } 
} 

这使我可以利用'eth0'作为一个全局变量。