javasnmpsnmp4j

Get all oid of network printer with snmp4j


I'm trying to get all the oid information of an specific device knowing the ip address,in this case a HP LaserJet Pro M404dw network printer, something similar to what I get running this snmpwalk command: snmpwalk -v 2c -c public ipaddress

snmpwalk output

This is what I've tried:

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;
    }    
}

The problem is that the response I get is null, seems that this method needs for me to pass a specific OID to get the value, but what I want is to get all of the OID from that IP just like with the snmpwalk command.

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

Thanks in advance for your help.

------------------ Update -------------------------------

I'v been doing some test and I found that the script does work with another printer, a Kyocera ECOSYS M2035dn. This is the response:

RESPONSE[requestID=1201694585, errorStatus=Success(0), errorIndex=0, 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]]

I don't know what I'm missing here but is really strange that it works with some devices and it doesn't with other.


Solution

  • I found a way to get all the oid I needed,

    Instead of sending the snmp request with: ResponseEvent responseEvent = snmp.send(pdu, target);

    You need to use: List<TreeEvent> events = treeUtils.getSubtree(target, new OID(tableOid)); that allows to fecth the OID subtree.

    This is an example of the code I endup using:

    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;
    }