How to set up SNMPV3 target with context name

In SNMP V2c we use Community String Indexing to specify a vlan id as discussed in https://www.cisco.com/c/en/us/support/docs/ip/simple-network-management-protocol-snmp/13492-cam-snmp.html. For example to walk dot1dTpFdbTable (OID 1.3.6.1.2.1.17.4.3) for VLAN 62 on the switch with IP 10.10.200.25 we use

snmpwalk -c community@62 10.10.200.25 1.3.6.1.2.1.17.4.3

Using SNMP4J we have this working in the same way using the following code to modify the community String when setting up a SNMP V2C target:

private Target  getV2cTarget(String ipAddress, int port, SNMPv2cCredential snmPv2cCredential, String snmpContext) {

String community = snmPv2cCredential.getCommunityName();

if (snmpContext != null && !snmpContext.isEmpty()) {
    community = community + "@" + snmpContext;
}

Address targetAddress = UdpAddress.parse(ipAddress + "/" + port);
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString(community));

target.setAddress(targetAddress);
... etc.

For SNMP V3 we can modify the Unix snmpwalk command as follows:

snmpwalk -v3  -l authPriv -u user -a md5 -A "AuthPassword"  -x AES -X "PrivPassword" -n vlan-62 10.10.200.25 1.3.6.1.2.1.17.4.3

Here we supply the -n option which allows us to set the context name in this case we supply vlan-62 to specify vlan 62 as the context.

This works fine using snmpwalk command. However my question is how do I translate this into SNMP4J? I cannot seem to find any way to set up a context name like this for an V3 target.

For SNMPv3 you use the ScopedPDU which has a member to contextName which is what you are looking for.

1 Like