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 For example to walk dot1dTpFdbTable (OID for VLAN 62 on the switch with IP we use

snmpwalk -c community@62

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));

... 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

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