Project

General

Profile

NL_arplist.pm

Perl Module to gather ARP table from given device - Randy Reitz, 12/14/2011 09:10 AM

 
1
# -*- mode: perl -*-
2
# Uses SNMP to extract ARP table info from an ethernet router or other device.
3
#
4
# Input: devicename, commstring
5
#
6
# Output (list): IPaddress MACaddress portname devicename timestamp
7
#
8
# timestamp: reflects time this program was executed
9
#
10
# Darryl Wohlt, Fermilab 
11

    
12
sub arplist {
13

    
14
use lib qw(/usr/lib/mrtg2);
15
use SNMP_Session "0.77";
16
use BER "0.77";
17
use SNMP_util "0.77";
18
$SNMP_Session::set_retries = 1;
19
$SNMP_Session::suppress_warnings = 2;
20
$SNMP_Session::set_timeout = 1;
21

    
22
my ($sw,$cstr) = @_;
23
my $IP_MIB_ifName = "1.3.6.1.2.1.31.1.1.1.1";
24
my $IP_MIB_ipNetToMediaPhysAddress = "1.3.6.1.2.1.4.22.1.2";
25
my (@output,@long,@intab,@fields,@patab,@lfields);
26
my ($ipaddr,$ipaddrlen,$uptime,$p,$timestamp,$entry,$intf,$leftfields,$intfndx,$intfname,$rightfields,$mac);
27
my (%ignore,%ifname);
28
@output='';
29
# DNS check
30
$ipaddr = gethostbyname($sw);
31
$ipaddrlen = length($ipaddr); 
32
@long = unpack('C4',$ipaddr);
33
if ($ipaddrlen == 0) {
34
  push @output, "Unknown name\n";
35
  return(@output);
36
}
37

    
38
# SNMP agent check
39
($uptime) = snmpget("$cstr\@$sw",'sysUptime');
40
if (!$uptime) {
41
  $p='';
42
  $p = ping_check($sw);
43
  if (!$p) {
44
    push @output, "No Ping response\n";
45
    return(@output);
46
  } else {
47
      push @output, "No SNMP response\n"; 
48
      return(@output);
49
  }
50
}
51
%ignore = ();
52
$ignore{"127.0.0"}=1;
53
$timestamp = qx(date +%Y\/%m\/%d\/%H:%M);
54
chomp $timestamp;
55

    
56
# Walk the interface table
57
(@intab) = snmpwalk("$cstr\@$sw",$IP_MIB_ifName);
58
foreach $intf (@intab) {
59
  @fields = split(":",$intf);
60
  $ifname{$fields[0]} = $fields[1];
61
}
62

    
63
# Walk the IP Net to Media Physical Address table
64
(@patab) = snmpwalk("$cstr\@$sw",$IP_MIB_ipNetToMediaPhysAddress);
65
foreach $entry (@patab) {
66
  $leftfields = substr($entry,0,index($entry,":")); # See note below
67
  @lfields = split(/\./,$leftfields);
68
  unless ($ignore{"$lfields[1].$lfields[2].$lfields[3]"}) {
69
    $rightfields = substr($entry,index($entry,":")+1);
70
    $mac = unpack('H12',$rightfields);
71
    push @output, "$lfields[1].$lfields[2].$lfields[3].$lfields[4] $mac $ifname{$lfields[0]} $sw $timestamp\n";
72
  }
73
}
74

    
75
# NOTE
76
# Dont change the code to split(":",$patab[$p]) because it messes up
77
# whenever the binary MAC field ($rightfields) contains a printable ":".
78

    
79
return(@output);
80
}
81

    
82
1;