使用snmp4j获取网络打印机的所有OID

问题描述

我试图获取知道该IP地址的特定设备的所有信息,在本例中为HP LaserJet Pro M404dw网络打印机,类似于我运行此snmpwalk命令的过程:snmpwalk -v 2c -c public ipaddress

snmpwalk output

这是我尝试过的:

@H_404_8@public static PDU MensajeSNMP(String ip) throws IOException { String address = "udp:" + ip + "/161"; String community = "public"; DefaultUdpTransportMapping transport = new DefaultUdpTransportMapping(); Snmp snmp = new Snmp(transport); transport.listen(); CommunityTarget target = new CommunityTarget(); target.setAddress(GenericAddress.parse(address)); target.setCommunity(new OctetString(community)); target.setRetries(2); target.setTimeout(1500); target.setVersion(SnmpConstants.version2c); PDU pdu = new PDU(); pdu.add(new VariableBinding(new OID(""))); pdu.setType(PDU.GETBULK); pdu.setMaxRepetitions(200); pdu.setNonRepeaters(0); ResponseEvent responseEvent = snmp.send(pdu,target); PDU response = responseEvent.getResponse(); return response; } }

问题是我得到的响应为空,似乎该方法需要我传递特定的OID来获取值,但是我想要的是像从snmpwalk一样从该IP获取所有OID命令。

@H_404_8@RESPONSE[requestID=1285053186,errorStatus=Success(0),errorIndex=0,VBS[]]

预先感谢您的帮助。

------------------更新---------------------------- ---

我正在做一些测试,发现该脚本确实可以与另一台打印机,京瓷ECOSYS M2035dn一起使用。这是响应:

@H_404_8@RESPONSE[requestID=1201694585,VBS[1.3.6.1.2.1.1.1.0 = KYOCERA Document Solutions Printing System; 1.3.6.1.2.1.1.2.0 = 1.3.6.1.4.1.1347.41; 1.3.6.1.2.1.1.3.0 = 1 day,23:15:38.96; 1.3.6.1.2.1.1.4.0 = ; 1.3.6.1.2.1.1.5.0 = ; 1.3.6.1.2.1.1.6.0 = Summan Tecnicos; 1.3.6.1.2.1.1.7.0 = 12; 1.3.6.1.2.1.2.1.0 = 1; 1.3.6.1.2.1.2.2.1.1.1 = 1; 1.3.6.1.2.1.2.2.1.2.1 = eth0; 1.3.6.1.2.1.2.2.1.3.1 = 6; 1.3.6.1.2.1.2.2.1.4.1 = 1500; 1.3.6.1.2.1.2.2.1.5.1 = 1000000000; 1.3.6.1.2.1.2.2.1.6.1 = 00:17:c8:23:16:c0; 1.3.6.1.2.1.2.2.1.7.1 = 1; 1.3.6.1.2.1.2.2.1.8.1 = 1; 1.3.6.1.2.1.2.2.1.9.1 = 0:00:00.00; 1.3.6.1.2.1.2.2.1.10.1 = 63665566; 1.3.6.1.2.1.2.2.1.11.1 = 746441; 1.3.6.1.2.1.2.2.1.12.1 = 0; 1.3.6.1.2.1.2.2.1.13.1 = 0; 1.3.6.1.2.1.2.2.1.14.1 = 0; 1.3.6.1.2.1.2.2.1.15.1 = 0; 1.3.6.1.2.1.2.2.1.16.1 = 41207085; 1.3.6.1.2.1.2.2.1.17.1 = 168517; 1.3.6.1.2.1.2.2.1.18.1 = 0; 1.3.6.1.2.1.2.2.1.19.1 = 0; 1.3.6.1.2.1.2.2.1.20.1 = 0; 1.3.6.1.2.1.2.2.1.21.1 = 0; 1.3.6.1.2.1.2.2.1.22.1 = 0.0; 1.3.6.1.2.1.3.1.1.1.1.1.172.23.254.1 = 1; 1.3.6.1.2.1.3.1.1.1.1.1.172.23.254.170 = 1; 1.3.6.1.2.1.3.1.1.2.1.1.172.23.254.1 = f4:ce:46:a6:05:ab; 1.3.6.1.2.1.3.1.1.2.1.1.172.23.254.170 = 5c:e0:c5:0d:ea:30; 1.3.6.1.2.1.3.1.1.3.1.1.172.23.254.1 = 172.23.254.1; 1.3.6.1.2.1.3.1.1.3.1.1.172.23.254.170 = 172.23.254.170; 1.3.6.1.2.1.4.1.0 = 2; 1.3.6.1.2.1.4.2.0 = 64; 1.3.6.1.2.1.4.3.0 = 173835; 1.3.6.1.2.1.4.4.0 = 0; 1.3.6.1.2.1.4.5.0 = 0; 1.3.6.1.2.1.4.6.0 = 0; 1.3.6.1.2.1.4.7.0 = 0; 1.3.6.1.2.1.4.8.0 = 0; 1.3.6.1.2.1.4.9.0 = 171711; 1.3.6.1.2.1.4.10.0 = 159125; 1.3.6.1.2.1.4.11.0 = 0; 1.3.6.1.2.1.4.12.0 = 0; 1.3.6.1.2.1.4.13.0 = 0; 1.3.6.1.2.1.4.14.0 = 16; 1.3.6.1.2.1.4.15.0 = 8; 1.3.6.1.2.1.4.16.0 = 0; 1.3.6.1.2.1.4.17.0 = 20; 1.3.6.1.2.1.4.18.0 = 0; 1.3.6.1.2.1.4.19.0 = 41; 1.3.6.1.2.1.4.20.1.1.172.23.254.194 = 172.23.254.194; 1.3.6.1.2.1.4.20.1.2.172.23.254.194 = 1; 1.3.6.1.2.1.4.20.1.3.172.23.254.194 = 255.255.255.0; 1.3.6.1.2.1.4.20.1.4.172.23.254.194 = 1; 1.3.6.1.2.1.4.20.1.5.172.23.254.194 = -1; 1.3.6.1.2.1.4.21.1.1.0.0.0.0 = 0.0.0.0; 1.3.6.1.2.1.4.21.1.1.172.23.254.0 = 172.23.254.0; 1.3.6.1.2.1.4.21.1.2.0.0.0.0 = 1; 1.3.6.1.2.1.4.21.1.2.172.23.254.0 = 1; 1.3.6.1.2.1.4.21.1.3.0.0.0.0 = 1; 1.3.6.1.2.1.4.21.1.3.172.23.254.0 = 0; 1.3.6.1.2.1.4.21.1.4.0.0.0.0 = -1; 1.3.6.1.2.1.4.21.1.4.172.23.254.0 = -1; 1.3.6.1.2.1.4.21.1.5.0.0.0.0 = -1; 1.3.6.1.2.1.4.21.1.5.172.23.254.0 = -1; 1.3.6.1.2.1.4.21.1.6.0.0.0.0 = -1; 1.3.6.1.2.1.4.21.1.6.172.23.254.0 = -1; 1.3.6.1.2.1.4.21.1.7.0.0.0.0 = 172.23.254.1; 1.3.6.1.2.1.4.21.1.7.172.23.254.0 = 0.0.0.0; 1.3.6.1.2.1.4.21.1.8.0.0.0.0 = 4; 1.3.6.1.2.1.4.21.1.8.172.23.254.0 = 3; 1.3.6.1.2.1.4.21.1.9.0.0.0.0 = 2; 1.3.6.1.2.1.4.21.1.9.172.23.254.0 = 2; 1.3.6.1.2.1.4.21.1.10.0.0.0.0 = 0; 1.3.6.1.2.1.4.21.1.10.172.23.254.0 = 0; 1.3.6.1.2.1.4.21.1.11.0.0.0.0 = 0.0.0.0; 1.3.6.1.2.1.4.21.1.11.172.23.254.0 = 255.255.255.0; 1.3.6.1.2.1.4.21.1.12.0.0.0.0 = -1; 1.3.6.1.2.1.4.21.1.12.172.23.254.0 = -1; 1.3.6.1.2.1.4.21.1.13.0.0.0.0 = 0.0; 1.3.6.1.2.1.4.21.1.13.172.23.254.0 = 0.0; 1.3.6.1.2.1.4.22.1.1.1.172.23.254.1 = 1; 1.3.6.1.2.1.4.22.1.1.1.172.23.254.170 = 1; 1.3.6.1.2.1.4.22.1.2.1.172.23.254.1 = f4:ce:46:a6:05:ab; 1.3.6.1.2.1.4.22.1.2.1.172.23.254.170 = 5c:e0:c5:0d:ea:30; 1.3.6.1.2.1.4.22.1.3.1.172.23.254.1 = 172.23.254.1; 1.3.6.1.2.1.4.22.1.3.1.172.23.254.170 = 172.23.254.170; 1.3.6.1.2.1.4.22.1.4.1.172.23.254.1 = 3; 1.3.6.1.2.1.4.22.1.4.1.172.23.254.170 = 3; 1.3.6.1.2.1.4.23.0 = 0; 1.3.6.1.2.1.5.1.0 = 0; 1.3.6.1.2.1.5.2.0 = 0; 1.3.6.1.2.1.5.3.0 = 0; 1.3.6.1.2.1.5.4.0 = 0; 1.3.6.1.2.1.5.5.0 = 0]]

我不知道我在这里缺少什么,但是很奇怪,它可以在某些设备上使用,而不能在其他设备上使用。

解决方法

我找到了一种获取所需所有oid的方法,

而不是通过以下方式发送snmp请求:ResponseEvent responseEvent = snmp.send(pdu,target);

您需要使用:List<TreeEvent> events = treeUtils.getSubtree(target,new OID(tableOid));,它可以影响OID子树。

这是我最终使用的代码示例:

public static Map<String,String> doWalk(String tableOid,Target target) throws IOException {

    String address = "udp:" + ip + "/161";
    String community = "public";        

    CommunityTarget target = new CommunityTarget();
    target.setAddress(GenericAddress.parse(address));
    target.setCommunity(new OctetString(community));
    target.setRetries(2);
    target.setTimeout(1500);
    target.setVersion(SnmpConstants.version2c);

    Map<String,String> result = new TreeMap<>();
    TransportMapping transport = new DefaultUdpTransportMapping();
    Snmp snmp = new Snmp(transport);
    transport.listen();

    TreeUtils treeUtils = new TreeUtils(snmp,new DefaultPDUFactory());
    List<TreeEvent> events = treeUtils.getSubtree(target,new OID(tableOid));
    if (events == null || events.size() == 0) {
        System.out.println("Error: Unable to read table...");
        return result;
    }

    for (TreeEvent event : events) {
        if (event == null) {
            continue;
        }
        if (event.isError()) {
            System.out.println("Error: table OID [" + tableOid + "] " + event.getErrorMessage());
            continue;
        }

        VariableBinding[] varBindings = event.getVariableBindings();
        if (varBindings == null || varBindings.length == 0) {
            continue;
        }
        for (VariableBinding varBinding : varBindings) {
            if (varBinding == null) {
                continue;
            }
             
            result.put("." + varBinding.getOid().toString(),varBinding.getVariable().toString());
        }

    }
    snmp.close();

    return result;
}