• SnmpTerminal


    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;
        }
    }
  • 相关阅读:
    js 手机端触发事事件、javascript手机端/移动端触发事件
    行高引起的行内块级元素间距
    js实现复制功能
    encodeURI、encodeURIComponent、decodeURI、decodeURIComponent的区别
    CSS动画总结效果
    CSS属性之word-break:break-all强制性换行
    在handlebars.js {{#if}}条件下的逻辑运算符解决方案
    js模版引擎handlebars.js实用教程——由于if功力不足引出的Helper
    垂直方向兼容显示的内容多少的情况样式Flex布局
    实现div里的img图片水平垂直居中
  • 原文地址:https://www.cnblogs.com/wellla/p/4200828.html
Copyright © 2020-2023  润新知