#!/usr/bin/perl # Change the above line to match the location of Perl on your system, # e.g. /usr/local/bin/perl or /usr/bin/perl. The command "which perl" # should provide its location. # # Name: get_arp # Version: 1.1 # Written by: JMC (support -at- moonpoint.com) # Created: 2/1/2006 # Last modified: 7/22/2007 # Purpose: Obtain the ARP table from a network device, such as # a Cisco router or Linux workstation. # Usage: get_arp [--nomac] hostname community_string # # Arguments: # 1. If "-nomac" is specified on the command line, then the MAC addresses # corresponding to the IP addresses in the ARP table will not be displayed. # 2. hostname should be the IP address or Fully Qualified Domain Name (FQDN) # for the device to be queried. # 3. community_string should be the read-only SNMP community string for the # device. # # Notes: # # 1. This script depends on the UCD SNMP tool snmpwalk being present # on the system on which the script is run. # 2. The latest version of this script is available at # http://support.moonpoint.com/downloads/computer_languages/perl/get_arp # 3. For further information, see # http://support.moonpoint.com/blog/blosxom/2006/02/01#get_arp # 4. For other Perl scripts, # see http://support.moonpoint.com//downloads/computer_languages/perl/ # Getopt::Long allows one to parse arguments on the command line # See http://aplawrence.com/Unix/perlgetopts.html and # http://www.unix.org.ua/orelly/perl/prog/ch07_035.htm for further # information use Getopt::Long; # The optional "--nomac" argument controls whether MAC addresses are # displayed along with their corresponding IP addresses. If "--help" # is included on the command line, print usage information and quit. &GetOptions("nomac" => \$nomac, "help" => \$help); # Display usage information if "--help" was entered on the command line if (defined($help)) { print "Usage: get_arp [--help] [--nomac] hostname community\n\n"; print "If the optional --nomac argument is supplied, only IP addresses\n"; print "will be displayed, rather than both IP addresses and their\n"; print "associated MAC addresses.\n"; exit(); } # There should be at least 2 arguments, the hostname or IP address and the # community string if ($#ARGV < 1) { die "Usage: get_arp [--help] [--nomac] hostname community\n"; } # # Use "-v 1" to avoid "couldn't find party database" or "Bad version # specified error messages. Previous releases of the UCD SNMP tools sent SNMPv2 # (classic queries, which relied on party configuration information being # available. With the current release, by default queries now use SNMPv2c, # which does not need any such party configuration. # In either case, it is possible to specify the use of SNMPv1 # instead by giving the application the option "-v 1". # # A reference on Object Identifiers (OID) can be found at # http://www.alvestrand.no/objectid/1.3.6.1.2.1.html # You can do a search for specific OIDs at that URL. # # snmpwalk usage: # snmpwalk $router $communitystring $querytype # # $router = IP address or name of the router # $communitystring = Community string # $querytype = ip.ipNetToMediaTable.ipNetToMediaEntry.ipNetToMediaPhysAddress # $router = $ARGV[0]; $communitystring = $ARGV[1]; $querytype = "ip.ipNetToMediaTable.ipNetToMediaEntry.ipNetToMediaPhysAddress"; $now = localtime(); $snmpresult = `snmpwalk -v 1 -c $communitystring $router $querytype`; # For a Cisco router, snmpresult will hold results in the form below at this # point: # # ip.ipNetToMediaTable.ipNetToMediaEntry.ipNetToMediaPhysAddress.6.192.168.0.5 # = 0:30:f2:ec:17:fc # ... # ip.ipNetToMediaTable.ipNetToMediaEntry.ipNetToMediaPhysAddress.6.192.168.0.5 # = 0:30:f2:ec:8b:fc # # For a Linux workstation, snmpresult will hold results in the form below # at this point: # # IP-MIB::ipNetToMediaPhysAddress.2.192.168.6.49 = STRING: 0:c0:7b:a2:a1:d3 # Remove all but the IP and MAC addresses $snmpresult =~ s/.*ipNetToMediaPhysAddress\.\d*\.//g; $snmpresult =~ s/STRING: //g; print "ARP table for $router on $now\n\n"; # If "--nomac" was entered on the command line, # don't display MAC addresses. At this point snmpresult holds a list # of IP addresses and their corresponding MAC addresses in the form # IP Address = MAC Address if (defined($nomac)) { $snmpresult =~ s/\s=\s.*:.*:.*:.*:.*:.*//g; } print "$snmpresult";