1 pièce(s) jointe(s)
problème d'affichage du résultat dans une classe qui utilise SNMP
J'ai un problème d'affichage du résultat dans une classe qui utilise SNMP.
Le résultat ne s'affiche que si je répète l'exécution deux ou trois fois.
Voilà la classe:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| package mainsnmp;
import java.io.IOException;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.event.ResponseListener;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;
public class test {
String str1="";
String res1="";
public String getDisk() throws IOException
{
{
Address targetAddress = GenericAddress.parse("udp:127.0.0.1/161");
TransportMapping transport = new DefaultUdpTransportMapping();
Snmp snmp = new Snmp(transport);
transport.listen();
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setAddress(targetAddress);
target.setRetries(2);
target.setTimeout(1000);
target.setVersion(SnmpConstants.version1);
PDU pdu = new PDU();
pdu.add(new VariableBinding(new OID(new int[] {1,3,6,1,2,1,25,2,3,1,5,1})));//taille C:\
pdu.setType(PDU.GET);
ResponseListener listener = new ResponseListener()
{
public void onResponse(ResponseEvent event)
{
((Snmp)event.getSource()).cancel(event.getRequest(), this);
str1+=event.getResponse();
String substr1 = str1.substring(str1.indexOf("VBS"), str1.indexOf(";"));
String substr3 = str1.substring(str1.indexOf(';'), str1.indexOf("]]"));
res1="(Version du OS) :" + (substr1.substring(substr1.indexOf('=') +1, substr1.length())).trim();
}
};
snmp.send(pdu, target, null, listener);
return res1;
}
}} |
Et voilà son Main:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| package mainsnmp;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException
{
test ts=new test();
System.out.println(ts.getDisk());
}
} |
Voilà mon projet et la bibliothèque SNMP4j pour ceux qui veulent tester le projet: