Enterprise Datacenter Management Voodoo
Posts tagged network switches
Server to Switch Port mapping with SNMP
May 20th
Let’s say you have a switch. And you are on a computer that is connected to the switch. You know the following:
- The name of the switch
- The SNMP community string and version the switch uses.
But, you’re too lazy to walk over to the data center to figure out which port you’re connected on.
Here’s what you do (assuming you’re running Red Hat or CentOS)
yum - y install net-snmp-utils
This gives you the snmpwalk command.
Now:
first, figure out your MAC address:
[root@n33 etc]# ifconfig eth0 eth0 Link encap:Ethernet HWaddr 00:A0:D1:E9:3E:DC inet addr:10.3.0.133 Bcast:10.3.255.255 Mask:255.255.0.0 inet6 addr: fe80::2a0:d1ff:fee9:3edc/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:972965 errors:0 dropped:0 overruns:0 frame:0 TX packets:637686 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:587009797 (559.8 MiB) TX bytes:100870077 (96.1 MiB) Memory:fcde0000-fce00000
So my Mac is 00:A0:D1:E9:3E:DC
Nice.
Now lets look for this mac address on the switch. Assuming my community string is ‘foobar’ and my switch is set up to do snmp version 1, and my switch name is switch1, I do:
snmpwalk -v 1 -c foobar switch1 SNMPv2-SMI::mib-2.17.4.3.1.1
When you do this, you’ll see a list of the nodes. Among them should be a Hex-STRING that matches your MAC address:
... SNMPv2-SMI::mib-2.17.4.3.1.1.0.160.209.233.62.220 = Hex-STRING: 00 A0 D1 E9 3E DC ...
Notice that the string:
0.160.209.233.62.220
Is the decimal representation of the MAC address:
00:A0:D1:E9:3E:DC
So you have verified one important piece of information: You are in fact connected on this switch!
Now figure out which port!
We use the decimal representation of the MAC address from this point on to find the port. Usually for me this works:
snmpwalk -v 1 -c foobar switch1 SNMPv2-SMI::mib-2.17.7.1.2.2.1.2
Among the output I get:
SNMPv2-SMI::mib-2.17.7.1.2.2.1.2.413.0.160.209.233.62.220 = INTEGER: 5
This tells me that my node is connected to port 5 on the switch.
Sometimes (on cisco switches and others) you may need to use a different approach for the mac-to-index values:
snmpwalk -v 1 -c foobar switch1 SNMPv2-SMI::mib-2.17.4.3.1.2
This is great, in that I never had to leave my chair to figure this out! The pounds pile up and my largeness increases. Anybody got a better/easier way to do this?