i have been trying to apply commands of snmp at java on Ubuntu, i find an example code at stackoverflow and code's github link: https://github.com/jineshmathewt/snmpbulkwalk/blob/master/snmpbulkwalk/src/TestSNMP.java But when i try to execute i m getting an error .
i would like to ask what am i doing wrong ? , to mention i can use get command on terminal and it works .
public class TestSNMP {
private static final String SNMPPORT = "161";
private static final int snmpVersion = SnmpConstants.version2c;
private int snmpTimeout = 500;
private int numRetries = 2;
public void doSNMPBulkWalk(String ipAddr, String commStr, String bulkOID, String operation) throws IOException {
Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
UserTarget targetV3 = null;
CommunityTarget targetV2 = null;
UsmUser user = null;
PDU request = null;
snmp.listen();
Address add = new UdpAddress(ipAddr + "/" + SNMPPORT);
if (snmpVersion == SnmpConstants.version2c || snmpVersion == SnmpConstants.version1) {
targetV2 = new CommunityTarget();
targetV2.setCommunity(new OctetString(commStr));
targetV2.setAddress(add);
targetV2.setTimeout(snmpTimeout);
targetV2.setRetries(numRetries);
targetV2.setVersion(snmpVersion);
targetV2.setMaxSizeRequestPDU(65535);
}
if (snmpVersion == SnmpConstants.version2c) {
request = new PDU();
//request.setMaxRepetitions(100);
//request.setNonRepeaters(0);
}
request.setType(PDU.GETBULK);
OID oID = new OID(bulkOID);
request.add(new VariableBinding(oID));
OID rootOID = request.get(0).getOid();
VariableBinding vb, ar[];
List<TreeEvent> l = null;
TreeUtils treeUtils = new TreeUtils(snmp, new DefaultPDUFactory());
if (snmpVersion == SnmpConstants.version2c) {
targetV2.setCommunity(new OctetString(commStr));
if (operation.equalsIgnoreCase("bulkwalk")) {
OID[] rootOIDs = new OID[1];
rootOIDs[0] = rootOID;
l = treeUtils.walk(targetV2, rootOIDs);
} else {
l = treeUtils.getSubtree(targetV2, rootOID);
}
}
//System.out.println(l);
System.out.println("size="+l.size());
for(TreeEvent t : l){
VariableBinding[] vbs= t.getVariableBindings();
for (int i = 0; (vbs != null) && i < vbs.length; i++) {
vb = vbs[i];
String s = vb.toString();
System.out.println(s);
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Date d1 = new Date();
TestSNMP snmpTest = new TestSNMP();
try {
snmpTest.doSNMPBulkWalk(args[0], args[1], args[2], args[3]);
} catch (IOException e) {
e.printStackTrace();
}
Date d2 = new Date();
System.out.println("Time Elapsed=" + (d2.getTime() - d1.getTime()));
}
}