码迷,mamicode.com
首页 > 其他好文 > 详细

SnmpTerminal

时间:2015-01-04 13:19:37      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

package com.network.snmp;

import java.io.IOException;
import java.util.Vector;

import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
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 SnmpTerminal {
    /**[SnmpConstants.version1,SnmpConstants.version2c,SnmpConstants.version3]*/
    private int version = SnmpConstants.version1;
    private static final int DEFAULT_SNMP_PORT = 161;
    private Snmp snmp = null;
    private Address targetAddress = null;
    /**
     * init snmp use default snmp port
     * @param ip
     * @throws IOException
     */
    public SnmpTerminal(String ip) throws IOException {
        this(ip, DEFAULT_SNMP_PORT);
    }
    /**
     * 
     * @param ip
     * @param port
     * @throws IOException
     */
    public SnmpTerminal(String ip, int port) throws IOException {
        targetAddress = GenericAddress.parse("udp:" + ip + "/" + port);
        TransportMapping<?> transport = new DefaultUdpTransportMapping();
        snmp = new Snmp(transport);
        transport.listen();
    }
    
    /**
     * 
     * @param community
     * @param oid
     * @return
     * @throws Exception
     */
    public String sendPDU(String community,int[] oid) throws Exception {
        StringBuffer buffer = new StringBuffer();
        //分区
        CommunityTarget target = new CommunityTarget();
        target.setCommunity(new OctetString(community));
        target.setAddress(targetAddress);
        // 通信不成功时的重试次数
        target.setRetries(3);
        // 超时时间
        target.setTimeout(3000);
        target.setVersion(version);
        // 创建 PDU
        PDU pdu = new PDU();
        pdu.add(new VariableBinding(new OID(oid)));
        // MIB的访问方式
        pdu.setType(PDU.GET);
        // 向Agent发送PDU,并接收Response
        ResponseEvent respEvnt = snmp.send(pdu, target);
        // 解析Response
        if (respEvnt != null && respEvnt.getResponse() != null) {
            @SuppressWarnings("unchecked")
            Vector<VariableBinding> recVBs = (Vector<VariableBinding>) respEvnt.getResponse().getVariableBindings();
            for (int i = 0; i < recVBs.size(); i++) {
                VariableBinding recVB = recVBs.elementAt(i);
                buffer.append(recVB.getVariable());
            }
        }
        return buffer.toString();
    }
    public int getVersion() {
        return version;
    }
    public void setVersion(int version) throws Exception {
        if(version != SnmpConstants.version1 && version != SnmpConstants.version2c && version != SnmpConstants.version3)
            throw new Exception("Unsupported snmp protocol version");
        this.version = version;
    }
}

 

SnmpTerminal

标签:

原文地址:http://www.cnblogs.com/wellla/p/4200828.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!