SNMP4J添加用户

发布于 2024-11-26 16:17:03 字数 655 浏览 5 评论 0原文

我一直在做一些非常基本的 SNMP4J 编程。我想做的就是发送一个简单的“获取”请求,但到目前为止我的响应为空。我打开wireshark,发现在简单网络管理协议下,我的msgUserName 为空,我需要填充它。

我以为我已经使用以下代码设置了它:

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

我的处理方式是否错误?如果没有,如何设置在我的wireshark 的get-request 转储中看到的msgUserName?我对 SNMP 很陌生,所以我基本上是在运行示例。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒冷纷飞旳雪 2024-12-03 16:17:03

这是一个有效的 snmpset,您可以以相同的方式编写 snmp get。Snmp4j v2 和 v3 不使用相同的 api 类。

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


    }

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


    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文