Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 带有社区字符串索引的snmp4j snmpwalk_Java_Snmp4j - Fatal编程技术网

Java 带有社区字符串索引的snmp4j snmpwalk

Java 带有社区字符串索引的snmp4j snmpwalk,java,snmp4j,Java,Snmp4j,如何使用snmp4j和社区字符串索引进行snmpwalk 我可以通过如下更改社区字符串来创建社区字符串索引public@123 (123是vlanId) 但这只适用于snmpget!!??: public ResponseEvent get(OID oids) throws IOException { PDU pdu = new PDU(); pdu.add(new VariableBinding(oid)); pdu.setType(PDU.GETNEXT);

如何使用snmp4j和社区字符串索引进行snmpwalk

我可以通过如下更改社区字符串来创建社区字符串索引public@123 (123是vlanId)

但这只适用于snmpget!!??:

public ResponseEvent get(OID oids) throws IOException {
    PDU pdu = new PDU();
    pdu.add(new VariableBinding(oid));
    pdu.setType(PDU.GETNEXT);
    ResponseEvent event = snmp.send(pdu, getTarget(), null);
}

private Target getTarget() {
    Address targetAddress = GenericAddress.parse(sw.getAddress());
    CommunityTarget target = new CommunityTarget();
    target.setCommunity(new OctetString(communityString));
    target.setAddress(targetAddress);
    target.setRetries(2);
    target.setTimeout(1500);
    target.setVersion(SnmpConstants.version2c);
    return target;
}
但是当我试着做这样的snmpwalk时,我得到了一个暂停

public HashMap<String, String> snmpWalk(String strOid) throws IOException {
    OID oid = new OID(strOid);
    TreeUtils treeUtils = new TreeUtils(snmp, new DefaultPDUFactory());
    HashMap<String, String> snmpResult = new HashMap<String, String>();
    List<TreeEvent> events = treeUtils.getSubtree(getTarget(), oid);
    // some more code ...
}
public HashMap snmpWalk(字符串strOid)抛出IOException{
OID=新OID(strOid);
TreeUtils TreeUtils=新树(snmp,new DefaultPDUFactory());
HashMap snmpResult=新HashMap();
List events=treeUtils.getSubtree(getTarget(),oid);
//更多的代码。。。
}

我通过使用GETBULK编写一个SNMP walk方法修复了这个问题

设置maxRepetitions和maxSizeResponsePDU非常重要我使用以下值:

/** maxRepetitions needs to be set for BULKGET to work<br>
 * it defines the maximum lines/results returned for one request. 
 */
private int maxRepetitions = 50;

/** maxSizeResponsePDU needs to be set for BULKGET to work */
private int maxSizeResponsePDU = 65535;

public HashMap<String, String> snmpWalk (String startOid) throws IOException{
    //String startOid = "1.3.6.1.4.1.9.9.46.1.3.1.1.4.1"; 
    String oid = startOid;
    HashMap<String, String> varBindings = new HashMap<String, String>();

    while (oid.startsWith(startOid)) {
        PDU pdu = getVariableBinding(new OID(oid), PDU.GETBULK);
        if (pdu == null || pdu.size() == 0) return varBindings;

        for (int i=0; i<pdu.size(); i++) {
            VariableBinding var = pdu.get(i);
            if (var == null) return varBindings;

            oid = var.getOid().toString();
            if (oid.startsWith(startOid)) {
                varBindings.put(oid, var.getVariable().toString());
            } else {
                return varBindings;
            }
        }
    }
    return varBindings;
}

/**
 * Method which takes a single OID and returns the response from the agent
 * as a String.
 * 
 * @param oid
 * @return
 * @throws IOException
 */
public PDU getVariableBinding(OID oid, int type) throws IOException {
    ResponseEvent event = get(new OID[] { oid }, type);

    if (event == null || event.getResponse() == null) {
        warn(oid);
        return null;
    }

    return event.getResponse();
}

public ResponseEvent get(OID oids[], int type) throws IOException {
    PDU pdu = new PDU();
    for (OID oid : oids) {
        pdu.add(new VariableBinding(oid));
    }
    pdu.setType(type);

    pdu.setMaxRepetitions(maxRepetitions); // This makes GETBULK work as expected

    ResponseEvent event = snmp.send(pdu, getTarget(), null);
    if (event != null) {
        return event;
    }
    throw new RuntimeException("GET timed out");
}

private Target getTarget() {
    Address targetAddress = GenericAddress.parse(sw.getAddress());
    CommunityTarget target = new CommunityTarget();
    target.setCommunity(new OctetString(communityString));
    target.setAddress(targetAddress);
    target.setRetries(2);
    target.setTimeout(3000);
    target.setVersion(SnmpConstants.version2c);

    target.setMaxSizeRequestPDU(maxSizeResponsePDU); // This makes GETBULK work as expected

    return target;
}
/**需要设置最大重复次数才能开始工作
*它定义了一个请求返回的最大行数/结果数。 */ 私有整数最大重复次数=50; /**需要设置maxSizeResponsePDU以使BULKGET正常工作*/ private int maxSizeResponsePDU=65535; 公共HashMap snmpWalk(字符串startOid)引发IOException{ //字符串startOid=“1.3.6.1.4.1.9.9.46.1.3.1.1.4.1”; 字符串oid=星形ID; HashMap varBindings=新HashMap(); while(oid.startsWith(startOid)){ PDU PDU=getVariableBinding(新OID(OID),PDU.GETBULK); if(pdu==null | | pdu.size()==0)返回varBindings; 对于(int i=0;i和SNMP4J,有一个已经提供了您在这里实现的“漫游”功能,并为maxrepations和nonRepeaters字段设置了合理的默认值