jNetLib/src/jnet/lib/snmp/SNMPTester.java

57 lines
1.9 KiB
Java

package jnet.lib.snmp;
import org.snmp4j.*;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.*;
import org.snmp4j.transport.*;
public class SNMPTester {
private String address;
private String community;
private int version;
public SNMPTester(String address, String port, String community, int version) {
this.address = address+"/"+port;
this.community = community;
this.version = version;
}
public boolean testSnmp() {
try {
// Nastavení SNMP agenta
TransportMapping transport = new DefaultUdpTransportMapping();
Snmp snmp = new Snmp(transport);
transport.listen();
// Cílový hostitel a community string
Target target = new CommunityTarget();
((CommunityTarget) target).setCommunity(new OctetString(this.community));
target.setVersion(this.version);
target.setAddress(new UdpAddress(this.address));
target.setRetries(3); // Počet pokusů
target.setTimeout(5000); // Timeout v milisekundách
// Odeslání SNMP požadavku na systémovou OID (sysDescr)
PDU request = new PDU();
request.add(new VariableBinding(SnmpConstants.sysDescr)); // OID pro systémový popis
request.setType(PDU.GET);
// Odeslání požadavku
ResponseEvent response = snmp.get(request, target);
snmp.close(); // Ukončení SNMP komunikace
// Zpracování odpovědi
if (response != null && response.getResponse() != null) {
PDU pdu = response.getResponse();
return true; // Funkční SNMP
} else {
return false; // Nefunkční SNMP
}
} catch (Exception e) {
return false; // Chyba komunikace
}
}
}