How do I control my program?
Hi,
The program below works fine, except the control is all out of whack. I would like to control the program flow from the main method. Hence call the getSNMP() and store the result of fixResponseString() to a global variable.
But I keep on receiving error when I try to accomplish this. The error is snmp4j.java:67: non-static method fixResponseString(java.lang.String) cannot be referenced from a static context. This is probably very common on here, but I need help to separate my program so it is all controlled from the main method.
Cheers,
import org.snmp4j.*;
import org.snmp4j.PDU;
import org.snmp4j.asn1.*;
import org.snmp4j.event.*;
import org.snmp4j.log.*;
import org.snmp4j.mp.*;
import org.snmp4j.security.*;
import org.snmp4j.smi.*;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.tools.console .*;
import org.snmp4j.transport.*;
import org.snmp4j.util.*;
publicclass snmp4j{
publicstaticvoid main( String [] args )throws Exception{
getSNMP();
}
//Retrieves the SNMP variables based on the string OID and stringAddress
staticvoid getSNMP()throws Exception{
String stringOID ="1.3.6.1.2.1.1.5.0";
String stringAddress ="10.31.9.41/161";
// Target address
Address targetAddress =new UdpAddress (stringAddress);
OID targetOID =new OID(stringOID);
/*
* 1. PDU and Communityaddress
*PDU instance
*/
PDU requestPDU =new PDU();
requestPDU.add(new VariableBinding(targetOID));// sysName
requestPDU.setType(PDU.GET);
// Comunity Target
CommunityTarget target =new CommunityTarget();
target.setCommunity(new OctetString("liepsi73"));
target.setAddress(targetAddress);
target.setTimeout(500);// set timeout to 500 milliseconds -> 2*500ms = 1s total timeout
target.setVersion(SnmpConstants.version1);
/*
* 2. Classes for SNMP message sending (command generation)
* 3. Classes for SNMP message dispatching (command responding)
*/
TransportMapping transport =new DefaultUdpTransportMapping();
Snmp snmp =new Snmp(transport);
transport.listen();
ResponseEvent response = snmp.send(requestPDU, target);
if (response.getResponse() ==null){
System.out.println("Request timed out");
}else{
// dump response PDU
// print the response and call the fixResponseMethod
//SNMPresult = (response.getResponse().toString());
System.out.println(response.getResponse().toString());
fixResponseString(response.getResponse().toString());
}
}
// Manipulated the output of the retrieved snmp message.
staticvoid fixResponseString(String s){
s = s.substring(72);// remove everything before "VBS["
int i = s.indexOf("=");// remove everything before "="
s = s.substring(i+2);// remove "= "
int length = s.length();// get the length
s = s.substring(0, length-2);// remove last two characters
System.out.println(s);
}
}

