|
To set any value for a particular cell in the table, the row and column index should be known. The value can be set only if the data for the row, which contains the cell, is fetched by the table before a new value is set. To modify data in a SNMP table, both High-Level API and Low-Level API can be used.
The ID column represents the index column.
| ID | managerHost | managerPort |
|---|---|---|
|
3 |
localhost |
161 |
|
4 |
server |
1030 |
|
5 |
printer |
8001 |
|
6 |
switch |
8080 |
To set a new value to any cell:
Using SnmpTarget class
The OID of the cell, the columnOID appended by the row index, is set using setObjectID() method. Then the snmpSet() method can be used to set the value.
The following code snippet can be used to change the value of the cell 'switch' to a new value 'value' in the sample table.
|
SnmpTarget target=new SnmpTarget(); target.setObjectID("managerHost.6"); target.snmpSet("value"); //sets the new value |
Using SnmpTable class
The following methods can be used to set a new value for a cell.
|
void setValueAt(java.lang.Object aValue, int rowIndex, int columnIndex) or void setCellValue(String tableOID, java.lang.Object aValue, int rowIndex, int columnIndex) |
where the row and column index represent the position of the row and column in the table.
The new value that needs to be set can be a String, SnmpVarBind or SnmpVar object.
The following methods can be used to set the managerHost value 'switch'.
|
table.setTableOID("tableOID"); table.setValueAt("newvalue", 3,1); or table.setCellValue("tableOID", "newvalue", 3,1) |
Here 3 represent the row position and 1 represents the column position in the table. For the managerPort value at 2nd row, 3rd column (1030), the row and column indices are 1 and 2 respectively. This value can be changed using setValueAt ("5000", 1, 2) or setCellValue ("tableOID", "5000", 1, 2). View the complete example present in <tutorials/highlevelapi/ModifyTable.java>.
To set any value for a particular cell, the SnmpVarBind object with the OID of the cell, which is the columnOID appended by the row index for which new value has to be set, and the new value can be added to the PDU. Then the request can be sent using the syncSend() method in SnmpSession class.
| ID | managerHost | managerPort |
|---|---|---|
|
3 |
localhost |
161 |
|
4 |
server |
1030 |
|
5 |
printer |
8001 |
|
6 |
switch |
8080 |
For example, the following code snippet is used to change the managerHost column value 'switch' to a new value 'value' where the numeric OIDs for these columnOIDs be .1.3.6.1.4.1.2.2.1.1, .1.3.6.1.4.1.2.2.1.2, .1.3.6.1.4.1.2.2.1.3.
|
SnmpPDU pdu=new SnmpPDU(); pdu.setCommand(SnmpAPI.SET_REQ_MSG); SnmpOID oid=new SnmpOID(".1.3.6.1.4.1.2.2.1.2.6"); SnmpVar var=SnmpVar.createVariable("value",SnmpAPI.STRING); SnmpVarBind varbind = new SnmpVarBind(oid, var); pdu.addVariableBinding(varbind); session.syncSend(pdu); |
View the complete example present in <lowlevelapi ModifyTable.java>.
|