Search code examples
snmpsnmp4j

How to add rows to a MIB-table in snmp4j


I am writing a program which is supposed to read and write MAC-addresses from and to the white-list of a local router.

I have managed to read the current table content of this so called "wlanACLTable", but I can't manage to add another row to this table using SNMP.

I searched for examples but all of them were for scalar values.

The device I want to contact is a router, a W2002 from Teldat


Solution

  • To add a row, you need to write the data in a new row within the table. The OID to use is the Table OID+ the column + the IP + the IfIndex(the Network id you want to configure) + the row

    EG: 1.3.6.1.4.1.272.4.46.8.1 + ".1" + ".255.255.255.255.255.255" + ".200000" + ".1337"

    Table: Column 1: MAC (format: 6 Numbers 0-255) Column 2: IfIndex (Integer Column 3: Status row(enabled 1, Disabled 2, delete 3) Write it to a single PDU just like

    pdu.addAll(new VariableBinding[]{new VariableBinding(new OID(Table_OID + ".1"+"."+stringMac +"."+IfIndex+"." + freeRow),mac),
                                     new VariableBinding(new OID(Table_OID + ".2"+"."+stringMac +"."+IfIndex+"." + freeRow),new Integer32(IfIndex)),
                                     new VariableBinding(new OID(Table_OID + ".3"+"."+stringMac +"."+IfIndex+"." + freeRow),new Integer32(1))});
    

    Finally, to write the data: Send them with a low level Set request, in this case SNMP V3:

    private Snmp snmp;
    
        public boolean executeSetRequest(String AccessName, String AccessPassword, ScopedPDU pdu) throws IOException {
    
                snmp.getUSM().addUser(new OctetString(AccessName), new UsmUser(new OctetString(AccessName), AuthMD5.ID, new OctetString(AccessPassword), PrivDES.ID, new OctetString(AccessPassword)));
    
                pdu.setType(ScopedPDU.SET);
                ResponseEvent response = snmp.send(pdu, getUserTarget(AccessName));
    
                if (response == null)
                    throw new RuntimeException("SET timed out");
    
                return true;
            }