Snmp mac2port

From 0xWIKI
Jump to: navigation, search

Contents

Setup Instructions

You need to have snmpwalk and perl for this to work.
In ubuntu you can run:
apt-get install snmp perl

Run the following commands

mkdir -p /scripts/snmp/switches

touch /scripts/snmp/hex2dec
touch /scripts/snmp/snmp-mac-query

touch /scripts/snmp/switches/cisco
touch /scripts/snmp/switches/dell
touch /scripts/snmp/switches/hp
touch /scripts/snmp/switches/h3c

chmod 755 /scripts/snmp/hex2dec
chmod 755 /scripts/snmp/snmp-mac-query

Make sure you configure the community in the snmp-mac-query script for the snmp community your switches listen on.
If your switches are locked down for SNMP to specific hosts, either configure an exception for an IP/host that will do these searches or run this from a whitelisted IP/host.
Add the IP for each of your switches for each vendor to the appropriate list on it's own line.

Syntax for running
/scripts/snmp/snmp-mac-query <switch vendor/list> <mac address>

The valid options for the <switch vendor/list>
cisco
dell
hp
h3c
all     #this queries all lists

The mac address must be in one of the following formats<br>
FF-FF-FF-FF-FF-FF
FF:FF:FF:FF:FF:FF

As an important note, when the script comes back with the switch and the ports the mac address shows up on, the mac address will show up on the trunk/uplink ports in addition to the host port. If you have your trunk/uplink ports documented (or use the last ports on the switch), it makes it easy to decipher which is the correct host port.

Script for converting hex to decimal for OID interpretation
  • /scripts/snmp/hex2dec
#!/usr/bin/perl

$in=shift @ARGV; @elements=split(/:/,$in); for $element (@elements) { print ".".hex($element); }
Script for querying the switches
  • /scripts/snmp/snmp-mac-query
#!/bin/bash

community=<your community name here>
version=2c
workingdir=/scripts/snmp

switch_hp=switches/hp
switch_dell=switches/dell
switch_h3c=switches/h3c
switch_cisco=switches/cisco

hex2dec=$(echo $2 | sed s/"-"/":"/g | while read line; do $workingdir/hex2dec $line; done)

hp () {
cat $workingdir/$switch_hp | while read line
do
	switch=$line
	echo $switch
	snmpwalk -v $version -c $community $switch 1.3.6.1.2.1.17.4.3.1.2 | grep "$hex2dec" | sed s/INTEGER/PORT/g | awk 'BEGIN { FS=" " }{ print $3 $4 }'
	echo "-----"
done
}

dell () {
cat $workingdir/$switch_dell | while read line
do
	switch=$line
	echo $switch
	snmpwalk -v $version -c $community $switch 1.3.6.1.2.1.17.7.1.2.2.1.2.1 | grep "$hex2dec" | sed s/INTEGER/PORT/g | awk 'BEGIN { FS=" " }{ print $3 $4 }'
	echo "-----"
done
}

h3c () {
cat $workingdir/$switch_h3c | while read line
do
	switch=$line
	echo $switch
	snmpwalk -v $version -c $community $switch 1.3.6.1.2.1.17.7.1.2.2.1.2 | grep "$hex2dec" | sed s/INTEGER/PORT/g | awk 'BEGIN { FS=" " }{ print $3 $4 }'
	echo "-----"
done
}

cisco () {
cat $workingdir/$switch_cisco | while read line
do
	switch=$line
	echo $switch
	snmpwalk -v $version -c $community $switch 1.3.6.1.2.1.17.4.3.1.2 | grep "$hex2dec" | sed s/INTEGER/PORT/g | awk 'BEGIN { FS=" " }{ print $3 $4 }'
	echo "-----"
done
}

all () {
hp
dell
h3c
cisco
}

$1
Personal tools