Hello monks,
I am trying to write a function that, given an array of IP addresses, will run ARP requests to find out which IPs are currently assigned to live hosts. I am in need of some pointing in the right direction for propper tools/modules to use. I tried Net::ARP however it primarily reads the ARP cache which does not provide up-to-date information. also it's packet construction requires a target MAC address. If I had the MAC address I wouldn't need to send the request ;).
What I actually need is to create broadcast packets of "Who has <ip address>" and then listen for the replies. I think Net::Pcap is probably half of what I need. If someone can point me to a good way to do the requests, and/or tell me if Net::Pcap is really what I should be messing with here, I'd appreciate it. Cheers!
UPDATE: Well as per my other post at
Net::Pcap garbage Output Pcap has not worked out well for me so far. I think I am going to try approaching this from a different angle in my obvious lack of full understanding of Pcap. I'm going to try using Net::Frame::Dump to tackle this problem instead.
Update #2: : It would appear that the garbage output I was getting was from the NetPacket decode functions and I was mistakingly attributing them to Pcap due to the decimal converted values it was giving me in the lookup command. I switched over to using Net::Frame::Dump::Online and Net::Frame::Simple. After some leg work I have gotten this to work. code attached if anyone cares to comment
#!/usr/bin/perl
use Net::ARP;
use Net::Netmask;
use Net::Frame::Simple;
use Net::Frame::Dump::Online;
my $dev= "eth1";
$interface= shift;
$ifconf= `ifconfig $dev`;
$ifconf=~ /\d+\.\d+\.\d+\.\d+/;
$ipdec= $&;
$ifconf=~ /Mask:\d+\.\d+\.\d+\.\d+/;
$nmaskdec= $&;
$nmaskdec=~s/Mask://;
my $netblock= $ipdec . ":" . $nmaskdec ;
$netmask=new Net::Netmask ($netblock);
my @iprange = $netmask->enumerate;
$arpDump=Net::Frame::Dump::Online->new(
dev => $dev,
filter => 'arp');
$arpDump->start;
my %livehosts;
my $reply;
$counter=0;
while ($counter<3){
for $ipts (@iprange){
Net::ARP::send_packet($dev,$ipdec,$ipts,"00:18:de:34:8e:7b","f
+f:ff:ff:ff:ff:ff","request");
}
until ($arpDump->timeout){
if ($next=$arpDump->next){
my $r=Net::Frame::Simple->newFromDump($next);
$opc=$r->ref->{ARP}->opCode;
next unless $opc == 2;
$livehosts{$r->ref->{ARP}->srcIp}=$r->ref->{ARP}->src;
}
}
$arpDump->timeoutReset;
$counter++;
}
foreach $key (keys %livehosts){
print "IP Address $key is up with mac of $livehosts{$key} \n";
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.