문제

I've been doing some very basic SNMP4J programming. All I want to do is send a simple "get" request but so far my responses have been null. I opened up wireshark and found that in the under Simple Network Management Protocol, my msgUserName is blank and I need that to be populated.

I thought I had set it using the following code:

Snmp snmp = new Snmp(transport);
USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
SecurityModels.getInstance().addSecurityModel(usm);
transport.listen();

UsmUser user = new UsmUser(new OctetString("SNMPManager"), AuthSHA.ID,new OctetString("password"),null,null);
// add user to the USM
snmp.getUSM().addUser(user.getSecurityName(), user);

Am I going about it the wrong way? If not, how do I set the msgUserName as seen in my wireshark dump of the get-request? I'm very new to SNMP, so I'm essentially running off examples.

도움이 되었습니까?

해결책

This is a working snmpset you can write snmp get same way.Snmp4j v2 and v3 not using same api classes.

 private void snmpSetV3(VariableBinding[] bindings) throws TimeOutException, OperationFailed {
        Snmp snmp = null;
        try {
            PDU pdu = new ScopedPDU();
            USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
            SecurityModels.getInstance().addSecurityModel(usm);
            snmp = new Snmp(new DefaultUdpTransportMapping());
            snmp.getUSM().addUser(new OctetString(Username), new UsmUser(new OctetString(Username), AuthMD5.ID, new OctetString(Password), AuthMD5.ID, null));


            ScopedPDU scopedPDU = (ScopedPDU) pdu;
            scopedPDU.setType(PDU.SET);
            scopedPDU.addAll(bindings);
            UserTarget target = new UserTarget();
            target.setAddress(new UdpAddress(IPAddress + "/" + Port));
            target.setVersion(version); //SnmpConstants.version3
            target.setRetries(retries);
            target.setTimeout(timeout);
            target.setSecurityLevel(securityLevel); //SecurityLevel.AUTH_NOPRIV
            target.setSecurityName(new OctetString(Username));
            snmp.listen();
            ResponseEvent response = snmp.send(pdu, target);
            if (response.getResponse() != null) {
                PDU responsePDU = response.getResponse();
                if (responsePDU != null) {
                    if (responsePDU.getErrorStatus() == PDU.noError) {
                        return;
                    }
                    throw new OperationFailed("Error: Request Failed, "
                            + "Error Status = " + responsePDU.getErrorStatus()
                            + ", Error Index = " + responsePDU.getErrorIndex()
                            + ", Error Status Text = " + responsePDU.getErrorStatusText());
                }
            }
            throw new TimeOutException("Error: Agent Timeout... ");
        } catch (IOException e) {
            throw new OperationFailed(e.getMessage(), e);

        } finally {
            if (snmp != null) {
                try {
                    snmp.close();
                } catch (IOException ex) {
                    _logger.error(ex.getMessage(), ex);
                }
            }
        }


    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top