ITzhangdaopin
Zhang Daopin
2016-10-09 08:51
采纳率: 70%
浏览 1.9k
已采纳

有熟悉snmpv3和snmp4j的大牛,可以指点一下小弟么?

刚开始接触snmp,领导让编写一个,获取远程服务器CPU,内存等信息的API
自己做了一部分,得到了一下结果信息:
Synchronize message from 135.251.218.92/161
request:GET[reqestID=1137261822, errorStatus=0, errorIndex=0, VBS[1.3.6.1.2.1.1.1.0 = Null]]
response:REPORT[reqestID=0, errorStatus=0, errorIndex=0, VBS[1.3.6.1.6.3.15.1.1.3.0 = 90]]
要想得到需求,应该怎么做?怎么去请求想要得到的信息数据?

 package mySNMPtest;

import java.io.IOException;

import org.snmp4j.*;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.event.ResponseListener;
import org.snmp4j.mp.MPv3;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.security.*;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;

public class SnmpUtil {
    private Snmp snmp = null;
    private String version = null;

    @SuppressWarnings("rawtypes")
    public SnmpUtil(String version) {
        try {
            this.version = version;
            TransportMapping transport = new DefaultUdpTransportMapping();
            snmp = new Snmp(transport);
            if (version.equals("3")) {
                // 设置安全模式
                USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
                SecurityModels.getInstance().addSecurityModel(usm);
            }
            // 开始监听消息
            transport.listen();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void sendMessage(Boolean syn, final Boolean bro, PDU pdu, String addr) throws IOException {
        // 生成目标地址对象
        Address targetAddress = GenericAddress.parse(addr);
        Target target = null;
        if (version.equals("3")) {
            // 添加用户
            snmp.getUSM().addUser(new OctetString("MD5DES"), new UsmUser(new OctetString("MD5DES"), AuthMD5.ID,
                    new OctetString("MD5DESUserAuthPassword"), PrivDES.ID, new OctetString("MD5DESUserPrivPassword")));

            target = new UserTarget();
            // 设置安全级别
            ((UserTarget) target).setSecurityLevel(SecurityLevel.AUTH_PRIV);
            ((UserTarget) target).setSecurityName(new OctetString("MD5DES"));
            target.setVersion(SnmpConstants.version3);
        }
        // 目标对象相关设置
        target.setAddress(targetAddress);
        target.setRetries(5);
        target.setTimeout(1000);

        if (syn.equals(true)) {
            // 发送报文 并且接受响应
            ResponseEvent response = snmp.send(pdu, target);
            // 处理响应
            System.out.println("Synchronize message from " + response.getPeerAddress() + "\nrequest:"
                    + response.getRequest() + "\nresponse:" + response.getResponse());
        } else {
            // 设置监听对象
            ResponseListener listener = new ResponseListener() {

                @Override
                public void onResponse(ResponseEvent event) {
                    if (bro.equals(false)) {
                        ((Snmp) event.getSource()).cancel(event.getRequest(), this);
                    }
                    // 处理响应
                    PDU request = event.getRequest();
                    PDU response = event.getResponse();
                    System.out.println("Asynchronise message from " + event.getPeerAddress() + "\nrequest:" + request
                            + "\nresponse:" + response);
                }

            };
            // 发送报文
            snmp.send(pdu, target, null, listener);
        }
    }

    public static void main(String[] args) {
        SnmpUtil manager = new SnmpUtil("3");
        // 构造报文
        // PDU pdu = new PDU();
        PDU pdu = new ScopedPDU();
        // 设置要获取的对象ID
        OID oids = new OID("1.3.6.1.2.1.1.1.0");// 1.3.6.1.2.1.1.1.0
        pdu.add(new VariableBinding(oids));
        // 设置报文类型
        // pdu.setType(PDU.GETNEXT);
        ((ScopedPDU) pdu).setContextName(new OctetString("priv"));
        try {
            // 发送消息 其中最后一个是想要发送的目标地址
            manager.sendMessage(true, false, pdu, "udp:135.251.218.92/161");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}



  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • ITzhangdaopin
    Zhang Daopin 2016-10-10 00:44
    已采纳

    你有写过类似的代码模块吗?能否让我参考一下呢?

    点赞 评论
  • oyljerry
    oyljerry 2016-10-10 00:01

    需要查看获取CPU等信息的oid。然后就调用snmp的get方法去获取这些oid的值

    点赞 评论

相关推荐