Demo entry 6640801

snmp4j

   

Submitted by anonymous on Sep 14, 2017 at 03:40
Language: Java. Code size: 6.0 kB.

package snmp.util;

import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
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;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class SnmpUtil {
    public static boolean isAlive(String ip, int port, String community, long timeout) throws IOException {
        Map<String, String> result = new HashMap();
        CommunityTarget target = new CommunityTarget();
        target.setCommunity(new OctetString(community));
        target.setAddress(GenericAddress.parse("udp:" + ip + "/" + port));
        target.setVersion(SnmpConstants.version2c);
        target.setTimeout(timeout);
        Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
        snmp.listen();
        PDU pdu = new PDU();
        pdu.setType(PDU.GETNEXT);
        pdu.add(new VariableBinding(new OID("")));
        ResponseEvent responseEvent = snmp.send(pdu, target);
        if (null == responseEvent.getResponse()) {
            return false;
        } else {
            return true;
        }
    }

    public static void sendTrap(String ip, int port, String community) throws IOException {
        Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
        PDU pdu = new PDU();
        VariableBinding v = new VariableBinding();
        v.setOid(SnmpConstants.linkUp);
        v.setVariable(new OctetString("Snmp Trap V2 Test"));
        pdu.add(v);
// set target
        CommunityTarget target = new CommunityTarget();
        target.setCommunity(new OctetString(community));
        target.setAddress(GenericAddress.parse("udp:" + ip + "/" + port));
// retry times when commuication error
        target.setRetries(2);
        target.setTimeout(1500);
        target.setVersion(SnmpConstants.version2c);
// send pdu, return response
        snmp.send(pdu, target);
    }

    public static Map<String, String> get(String ip, int port, String community, String oid) throws IOException {
        Map<String, String> result = new HashMap();
        CommunityTarget target = new CommunityTarget();
        target.setCommunity(new OctetString(community));
        target.setAddress(GenericAddress.parse("udp:" + ip + "/" + port));
        target.setVersion(SnmpConstants.version2c);
        Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
        snmp.listen();
        PDU pdu = new PDU();
        pdu.setType(PDU.GET);
        pdu.add(new VariableBinding(new OID(oid)));
        ResponseEvent responseEvent = snmp.send(pdu, target);
        if (null == responseEvent) {
            return null;
        }
        result.put(responseEvent.getResponse().get(0).getOid().toString(), responseEvent.getResponse().get(0).getVariable().toString());
        return result;
    }

    public static Map<String, String> getBulk(String ip, int port, String community, String oid, int num) throws IOException {
        Map<String, String> result = new HashMap();
        CommunityTarget target = new CommunityTarget();
        target.setCommunity(new OctetString(community));
        target.setAddress(GenericAddress.parse("udp:" + ip + "/" + port));
        target.setVersion(SnmpConstants.version2c);
        target.setTimeout(100000);
        Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
        snmp.listen();
        PDU pdu = new PDU();
        pdu.setType(PDU.GETBULK);
        pdu.setMaxRepetitions(num);
        pdu.add(new VariableBinding(new OID(oid)));
        ResponseEvent responseEvent = snmp.send(pdu, target);
        if (null == responseEvent.getResponse()) {
            return null;
        }
        for (VariableBinding variableBinding : responseEvent.getResponse().getVariableBindings()) {
            result.put(variableBinding.getOid().toString(), variableBinding.getVariable().toString());
        }
        return result;
    }

    public static Map<String, String> getSubtree(String ip, int port, String community, String oid) throws IOException {
        Map<String, String> result = new HashMap();
        CommunityTarget target = new CommunityTarget();
        target.setCommunity(new OctetString(community));
        target.setAddress(GenericAddress.parse("udp:" + ip + "/" + port));
        target.setVersion(SnmpConstants.version2c);
        Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
        snmp.listen();
        PDU pdu = new PDU();
        pdu.setType(PDU.GETNEXT);
        pdu.add(new VariableBinding(new OID(oid)));
        ResponseEvent responseEvent = snmp.send(pdu, target);
        if (null == responseEvent) {
            return null;
        }
        if (responseEvent.getResponse().get(0).getOid().toString().contains(oid)) {
            result.put(responseEvent.getResponse().get(0).getOid().toString(), responseEvent.getResponse().get(0).getVariable().toString());
            looper(snmp, responseEvent.getResponse().get(0).getOid().toString(), target, result, oid);
        }
        return result;
    }

    private static void looper(Snmp snmp, String oid, CommunityTarget target, Map<String, String> result, String _oid) throws IOException {
        PDU pdu = new PDU();
        pdu.setType(PDU.GETNEXT);
        pdu.add(new VariableBinding(new OID(oid)));
        ResponseEvent responseEvent = snmp.send(pdu, target);
        if (responseEvent.getResponse().get(0).getOid().toString().contains(_oid)) {
            result.put(responseEvent.getResponse().get(0).getOid().toString(), responseEvent.getResponse().get(0).getVariable().toString());
            looper(snmp, responseEvent.getResponse().get(0).getOid().toString(), target, result, _oid);
        }
    }
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).