I have to program a small tool that grabs the specific port number each Mac Adress is connected to on the Switch.
I have searched for a way to do this and found the following on another Forum:
"Step by step procedure;
1) Get INTEGER value of mac address;
snmpwalk -O0sUX -v2c -Cc -c netpublic 10.50.2.223 BRIDGE-MIB::dot1dTpFdbPort
dot1dTpFdbPort[STRING: 90:1b:0e:1e:dd:fb] = INTEGER: 557
2) Get port index of this integer;
snmpwalk -O0sUX -v2c -Cc -c netpublic 10.50.2.223 BRIDGE-MIB::dot1dBasePortIfIndex | grep 557
dot1dBasePortIfIndex = INTEGER: 594
3) Get physical port number from ifDescr
snmpwalk -O0sUX -v2c -Cc -c netpublic 10.50.2.223 ifDescr | grep 594
ifDescr = STRING: ge-0/0/44.0
Now to my question:
How do I best realize the running of these three OID's?
What is best to use to save the in-between results? Array? Arraylist?
My idea is to get the first and second list, map them together by the ID they have in common, and then get the third list and map my result to the third list, thereby having a direct list from MAC to Port.
The first List consists of the Mac adress as an Integer, for example: "126.96.36.199.214.154"
How do I convert this to the String representation of the Mac Adress, in ist natural form?
Tim Moores wrote:I know very little about SNMP, but snmp4j looks like a very capable library, although with a steep learning curve unless you know SNMP well already. Alternatively, if you want to automate running those CLI commands, that's what the ProcessBuilder class is for.
thanks for the reply. I have already built the app to be able to walk or get through single OID commands.
And you are right, the snmp4j is a great library. Im not very knowledgeable in it but im learning.
If so, the exact MIBs in use would vary by vendor. SNMP is not very good about universal MIBs outside of some common core ones.
In most cases, however, you should be able to get the number of ports as a MIB request, then query each one. In fact, you should be able to simply enumerate the ports and the index of the MAC MIB would typically itself be the physical port ID (allow for 1/0 offset). So no real need to stash info away in a collection.
On top of that, MAC addresses are frequently assigned sequentially, so all you'd really need to know on a lot of devices would be the number of ports and the lowest port's MAC address and you'd be able to compute the others.
On my devices, MAC addresses are returned in the conventional hex format: "01:23:45:67:89:ab", but if you're getting them in dotted decimal form, the easiest thing to do would be to split the string between decimal points to get the individual decimal numbers as strings, then convert each number to hexadecimal.