Java SNMP4J 入门

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3224687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:33:34  来源:igfitidea点击:

Getting started with SNMP4J

javasnmpsnmp4j

提问by joemoe

I need to make an agent in SNMP4J, but the documentation on how to get started is pretty poor. Does anyone have any experience with SNMP4J and could give me an idea on how to get started? Thanks.

我需要在 SNMP4J 中创建一个代理,但是关于如何开始的文档非常糟糕。有没有人对 SNMP4J 有任何经验并且可以给我一个如何开始的想法?谢谢。

采纳答案by hallidave

You can download the source code for SNMP4JAgent here:

您可以在此处下载 SNMP4JAgent 的源代码:

http://www.snmp4j.org/html/download.html

http://www.snmp4j.org/html/download.html

The source code includes a sample agent -- look in the org.snmp4j.agent.example package for all of the related classes.

源代码包括一个示例代理——在 org.snmp4j.agent.example 包中查找所有相关类。

http://www.snmp4j.org/agent/doc/org/snmp4j/agent/example/SampleAgent.html

http://www.snmp4j.org/agent/doc/org/snmp4j/agent/example/SampleAgent.html

One way of getting started would be to create an agent using the example code and then modify it to suit your needs. The JavaDoc describing each of the classes is a bit terse, but it's complete.

一种入门方法是使用示例代码创建代理,然后对其进行修改以满足您的需要。描述每个类的 JavaDoc 有点简洁,但它是完整的。

回答by Pinky

Here is a great link that describes the snmp class which is the core of snmp4j

这是一个很好的链接,描述了 snmp 类,它是 snmp4j 的核心

http://www.snmp4j.org/doc/org/snmp4j/package-summary.html

http://www.snmp4j.org/doc/org/snmp4j/package-summary.html

Also take a look at the SnmpRequest.java for a quick example

另请查看 SnmpRequest.java 的快速示例

回答by Abhinav P

Good documentation of SNMPv3 implementation using SNMP4j libraries is really hard to find. There are no working examples of SNMPv3 agents in the official documentation. I wrote a basic SNMP Agent that can connect using SNMPv3 protocol, and perform GET and SET operations on the server.

使用 SNMP4j 库的 SNMPv3 实现的良好文档真的很难找到。官方文档中没有 SNMPv3 代理的工作示例。我写了一个基本的SNMP 代理,可以使用 SNMPv3 协议进行连接,并在服务器上执行 GET 和 SET 操作。

import java.io.IOException;

import org.snmp4j.PDU;
import org.snmp4j.ScopedPDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.UserTarget;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.MPv3;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.security.AuthGeneric;
import org.snmp4j.security.AuthSHA;
import org.snmp4j.security.PrivAES128;
import org.snmp4j.security.PrivacyGeneric;
import org.snmp4j.security.SecurityModels;
import org.snmp4j.security.SecurityProtocols;
import org.snmp4j.security.USM;
import org.snmp4j.security.UsmUser;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.TransportIpAddress;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultTcpTransportMapping;
import org.snmp4j.transport.DefaultUdpTransportMapping;

public class SNMPV3Agent {

    private Address nmsIP;
    private String user;
    private String securityName;
    private String privacyPassword;
    private String authorizationPassword;
    private AuthGeneric authProtocol;
    private PrivacyGeneric privacyProtocol;
    private String protocol;

    private long timeOut = 1000L;
    private int noOfRetries = 2;

    private Snmp snmp;
    private UserTarget target;

    SNMPV3Agent(String ip, String protocol, int snmpPort, String username,
            String securityName, String privacyPassword, String authPassowrd,
            AuthGeneric authProtocol, PrivacyGeneric privacyProtocol) {

        nmsIP = GenericAddress.parse(protocol + ":" + ip + "/" + snmpPort);
        System.out.println("NMS IP set : " + nmsIP.toString());

        this.protocol = protocol;
        this.user = username;
        this.securityName = securityName;
        this.privacyPassword = privacyPassword;
        this.authorizationPassword = authPassowrd;
        this.authProtocol = authProtocol;
        this.privacyProtocol = privacyProtocol;

    }

    public static void main(String[] args) {

        SNMPV3Agent agent = new SNMPV3Agent("nms/server-ip", "udp", 162,
                "abhinav", "abhinav", "myprivpass", "myauthpass",
                new AuthSHA(), new PrivAES128());
        try {
            agent.startAgent();
            ResponseEvent response = agent
                    .snmpGetOperation(SnmpConstants.sysName);
            System.out.println(response.getResponse());
            // Similarly you can perform set operation.
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void startAgent() throws IOException {
        if (snmp == null) {
            TransportMapping<? extends TransportIpAddress> transport = null;
            if (protocol.equalsIgnoreCase("udp")) {
                System.out.println("UDP Protocol selected.");
                transport = new DefaultUdpTransportMapping();
            } else {
                System.out.println("TCP Protocol selected.");
                transport = new DefaultTcpTransportMapping();
            }
            snmp = new Snmp(transport);
            USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(
                    MPv3.createLocalEngineID()), 0);
            SecurityModels.getInstance().addSecurityModel(usm);
            transport.listen();
            snmp.getUSM().addUser(
                    new OctetString(user),
                    new UsmUser(new OctetString(securityName), authProtocol
                            .getID(), new OctetString(authorizationPassword),
                            privacyProtocol.getID(), new OctetString(
                                    privacyPassword)));

            target = createUserTarget();
        }

    }

    public ResponseEvent snmpSetOperation(VariableBinding[] vars)
            throws IOException {
        PDU setPdu = new ScopedPDU();
        for (VariableBinding variableBinding : vars) {
            setPdu.add(variableBinding);
        }
        return snmp.send(setPdu, target);
    }

    public ResponseEvent snmpGetOperation(OID oid) throws IOException {

        PDU getPdu = new ScopedPDU();
        getPdu.add(new VariableBinding(oid));
        return snmp.get(getPdu, target);
    }

    private UserTarget createUserTarget() {
        UserTarget target = new UserTarget();
        target.setAddress(nmsIP);
        target.setRetries(noOfRetries);
        target.setTimeout(timeOut);
        target.setVersion(3);
        target.setSecurityLevel(3);
        target.setSecurityName(new OctetString(securityName));
        return target;
    }

    public long getTimeOut() {
        return timeOut;
    }

    public void setTimeOut(long timeOut) {
        this.timeOut = timeOut;
    }

    public int getNoOfRetries() {
        return noOfRetries;
    }

    public void setNoOfRetries(int noOfRetries) {
        this.noOfRetries = noOfRetries;
    }
}

Adding other operations such as GETBulk will be relatively easy once you understand how GET and SET works. Let me know if you need more clarifications in the comments.

一旦您了解了 GET 和 SET 的工作原理,添加其他操作(如 GETBulk)将相对容易。如果您需要在评论中进行更多说明,请告诉我。