Switches, Hubs, and Modems
cancel
Showing results for 
Search instead for 
Did you mean: 

Find a port number from a MAC address using SNMP

Soukron
Occasional Visitor

Find a port number from a MAC address using SNMP

Hi folks.
I'm writing a little bash shell script to find which port is connected an computer using snmp and its MAC.

I actually have some OID's to do it, but they only works in CISCO switches, they don't work with two HP J4899B ProCurve Switch 2650.

Has anyone any idea?

Thanks,
Sergio
3 REPLIES
bergonz
Advisor

Re: Find a port number from a MAC address using SNMP

You should use the BRIDGE-MIB for that, which is a standard MIB supported by almost anything.

The OID for the port o a MAC address is 1.3.6.1.2.1.17.4.3.1.1, so you can do:

snmpwalk sw2524-1 .1.3.6.1.2.1.17.4.3.1.2
SNMPv2-SMI::mib-2.17.4.3.1.2.0.0.116.117.96.246 = INTEGER: 11
SNMPv2-SMI::mib-2.17.4.3.1.2.0.1.230.133.83.224 = INTEGER: 26
SNMPv2-SMI::mib-2.17.4.3.1.2.0.4.118.248.42.33 = INTEGER: 26

... and so on

The MAC address is encoded in six decimal labels in the instance, so

0.0.116.117.96.246

really means 00:00:74:75:60:F6

Doing that in a shell script can be tricy: you are probably going to use cut and bc.

Regards,
Bergonz
Soukron
Occasional Visitor

Re: Find a port number from a MAC address using SNMP

That's my function... it worked like in CISCO but i didn't realized.

function busca_en_router {
ip=$1
first=`snmpwalk -v 1 -Os $ip -c .1.3.6.1.2.1.17.4.3.1.1 | grep "$mac" | cut -d= -f 1 | cut -d. -f 7-`

if [ -n "$first" ]; then
Bridge=`snmpwalk -v 1 -Os $ip -c .1.3.6.1.2.1.17.4.3.1.2.$first | cut -d" " -f4`
ifIndex=`snmpwalk -v 1 -Os $ip -c .1.3.6.1.2.1.17.1.4.1.2.$Bridge | cut -d" " -f4`
ifName=`snmpwalk -v 1 -Os $ip -c .1.3.6.1.2.1.31.1.1.1.1.$ifIndex | cut -d" " -f4`
echo $equipo" "`echo $mac | tr -s " " ":"`" "$ip" "$ifName
fi
}

Call it this way:
busca_en_router "00 11 22 33 44 55 66"

Of course you have to change it and put down your community, your own ip, and your version (!!!).

And if you don't want to see as a result if the pc is attached to a gigabyte port (because it is really attached to a fastethernet port in other switch connected via this gigabyte port), add this "if" before the "echo", and the "fi" after, like that:

if [ "$ifName" != "Gi0/1" ] && [ "$ifName" != "Gi0/2" ]; then
echo $equipo" "`echo $mac | tr -s " " ":"`" "$ip" "$ifName
fi

It has helped me a lot to make a fast "net map" of my computers and switches.

And I'm sure it can be writed better... but works ;)

Thanks a lot!
Soukron
Occasional Visitor

Re: Find a port number from a MAC address using SNMP

I have a typo... sorry.

Add mac=$2 after ip=$1 ....