本文整理汇总了Java中org.snmp4j.Snmp.getNext方法的典型用法代码示例。如果您正苦于以下问题:Java Snmp.getNext方法的具体用法?Java Snmp.getNext怎么用?Java Snmp.getNext使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.snmp4j.Snmp
的用法示例。
在下文中一共展示了Snmp.getNext方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: snmpWalk
import org.snmp4j.Snmp; //导入方法依赖的package包/类
/**
* walk方式获取指定的oid value
*
* @param snmp
* @param target
* @param oid
* @return
* @throws IOException
*/
public static List<PDU> snmpWalk(Snmp snmp, Target target, String oid) throws IOException {
List<PDU> pduList = new ArrayList<>();
ScopedPDU pdu = new ScopedPDU();
OID targetOID = new OID(oid);
pdu.add(new VariableBinding(targetOID));
boolean finished = false;
while (!finished) {
VariableBinding vb = null;
ResponseEvent respEvent = snmp.getNext(pdu, target);
PDU response = respEvent.getResponse();
if (null == response) {
break;
} else {
vb = response.get(0);
}
// check finish
finished = checkWalkFinished(targetOID, pdu, vb);
if (!finished) {
pduList.add(response);
// Set up the variable binding for the next entry.
pdu.setRequestID(new Integer32(0));
pdu.set(0, vb);
}
}
return pduList;
}
示例2: main
import org.snmp4j.Snmp; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception
{
System.out.println("SNMP GET-NEXT Simple Request");
// Create TransportMapping and Listen
TransportMapping transport = new DefaultUdpTransportMapping();
transport.listen();
// Create Target Address object
CommunityTarget comtarget = new CommunityTarget();
comtarget.setCommunity(new OctetString(community));
comtarget.setVersion(snmpVersion);
comtarget.setAddress(new UdpAddress(ipAddress + "/" + port));
comtarget.setRetries(2);
comtarget.setTimeout(1000);
// Create the PDU object
PDU pdu = new PDU();
pdu.add(new VariableBinding(new OID(oidValue)));
pdu.setRequestID(new Integer32(1));
pdu.setType(PDU.GETNEXT);
// Create Snmp object for sending data to Agent
Snmp snmp = new Snmp(transport);
System.out.println("Sending GetNext Request to Agent ...");
ResponseEvent response = snmp.getNext(pdu, comtarget);
// Process Agent Response
if (response != null)
{
System.out.println("\nResponse:\nGot GetNext Response from Agent...");
PDU responsePDU = response.getResponse();
if (responsePDU != null)
{
int errorStatus = responsePDU.getErrorStatus();
int errorIndex = responsePDU.getErrorIndex();
String errorStatusText = responsePDU.getErrorStatusText();
if (errorStatus == PDU.noError)
{
System.out.println("Snmp GetNext Response for sysObjectID = " + responsePDU.getVariableBindings());
}
else
{
System.out.println("Error: Request Failed");
System.out.println("Error Status = " + errorStatus);
System.out.println("Error Index = " + errorIndex);
System.out.println("Error Status Text = " + errorStatusText);
}
}
else
{
System.out.println("Error: GetNextResponse PDU is null");
}
}
else
{
System.out.println("Error: Agent Timeout... ");
}
snmp.close();
}