in reply to Re: Fetching ARP table with NetPacket
in thread Fetching ARP table with NetPacket

In this case, GNU/Linux, I could use the arp command. Indeed I don't need this script to know "who's online", or anything like it, but I do need it because I wanted an example of how I could fetch the mac address of the network interfaces I have installed on my machine using code (it's a research work, by the way), and not internalizing another program such as system("arp -a"). I also ocurred me that it could be useful to fetch the entire arp table, so I asked the question as above :) I imagine I could fetch it from /proc. But I'm not sure how portable this solution is. I know it stands almost for the same as fetching from the "arp -a" results, but it's more tolerable from the point of view of this research (not my idea). Again, any help is appreciated.
  • Comment on Re: Re: Fetching ARP table with NetPacket

Replies are listed 'Best First'.
Re: Re: Re: Fetching ARP table with NetPacket
by hsinclai (Deacon) on Jun 01, 2004 at 16:58 UTC

    I'm sure there are shorter, much more elegant ways of doing this (hire a monk:), plus in a multi-interface system, you'd want more info about which "device" from the arp table.. but, here's a basic approach..
    #!/usr/bin/perl -w # kernels 2.4.x /proc/net/arp use strict; my $ipad; my $macad; my @arp; if ( -r "/proc/net/arp" ) { open(ARP,"</proc/net/arp"); @arp = <ARP>; close(ARP); } else { die("Cannot access arp in proc $!"); } foreach my $linein ( @arp ) { chomp($linein); next if $linein =~ /IP/i; $ipad = (split(/\s+/,$linein))[0]; $macad = (split(/\s+/,$linein))[3]; print "IP address $ipad has MAC Address $macad\n"; }

    to start with.. definitely not portable to Windows or BSD, just linux

    You could also split up each record getting a value for each field with something like
    ($ipad,$macad,$hwtype,$flags,$mask,$device) = split(/\s+/,$linein);
    -hsinclai