Zhang Daopin
2016-10-14 00:59
采纳率: 70%
浏览 2.1k
已采纳

有关java代码通过snmpv3协议,获取服务器设备信息的代码疑问?内附代码及测试结果

java代码通过snmp协议,获取服务器设备信息,使用常用的OID,例如1.3.6.1.2.1.1.5.0,获取不到任何信息。。

求大神,copy,测试,指点!谢谢!

 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());
            System.out.println("getUserObject:"+response.getUserObject());
        } 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.5.0");// 1.3.6.1.2.1.1.1.0
        pdu.add(new VariableBinding(oids));
        // 设置报文类型
        // pdu.setType(PDU.GETNEXT);
        ((ScopedPDU) pdu).setContextName(new OctetString("public"));

        try {
            // 发送消息 其中最后一个是想要发送的目标地址
            manager.sendMessage(true, false, pdu, "udp:135.251.218.92/161");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

测试结果如下:

 Synchronize message from 135.251.218.92/161
request:GET[reqestID=1256933277, errorStatus=0, errorIndex=0, VBS[1.3.6.1.2.1.1.5.0 = Null]]
response:REPORT[reqestID=0, errorStatus=0, errorIndex=0, VBS[1.3.6.1.6.3.15.1.1.3.0 = 266]]
getUserObject:UserTarget[address=135.251.218.92/161,version=3,timeout=1000,retries=5,securityLevel=3,securityModel=3,securityName=MD5DES,preferredTransports=null, authoritativeEngineID=]

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

3条回答 默认 最新

  • Zhang Daopin 2016-10-14 01:02
    已采纳

    这里“”1.3.6.1.2.1.1.5.0 = Null“”为什么?

    还有“”1.3.6.1.6.3.15.1.1.3.0 = 266“”是什么意思?

    点赞 打赏 评论
  • 圣西罗1118 2017-04-12 13:47

    我这里返回response.getResponse() 是null 用v1和 v2都是可以返回的,是啥原因?

    点赞 打赏 评论
  • Zhang Daopin 2017-04-13 00:48

    看你的设备是否支持snmpV3?或者设备是否正确配置snmpV3?

    点赞 打赏 评论

相关推荐 更多相似问题