2017-03-08 18 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'作爲一個全局變量。