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

snmp4j 编程

时间:2014-08-07 00:25:07      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   java   os   io   for   ar   

从www.snmp4j.org下载snmp4j的jar包,用eclipse新建一个java项目。将jar包导入工程(只有一个jar包),开始编程

一个简单的测试程序

//create target

//create pdu

//snmp.send

okay,上代码

package com.gxf.snmp;

import java.io.IOException;

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.Variable;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;

//create target
//create pdu
//snmp send

public class SNMP {

    public static void main(String[] args) {
        //create target
        Address address = GenericAddress.parse("udp:210.38.235.186/161");
        CommunityTarget target = new CommunityTarget();
        
        target.setCommunity(new OctetString("public"));
        target.setAddress(address);
        target.setTimeout(10000);//10s
        target.setVersion(SnmpConstants.version2c);
        //create pdu
        PDU pduSet = createSetPdu();
        PDU pudGet = createGetPdu();
        
        sendPdu(pudGet, target);
        sendPdu(pduSet, target);
        sendPdu(pudGet, target);
        
        
    }
    //1.3.6.1.2.1.1.5.0 sysName
    //create set pdu 
    public static  PDU createSetPdu(){
        PDU pduReturn = new PDU();
        
        pduReturn.setType(PDU.SET);
        VariableBinding variable = new VariableBinding(new OID("1.3.6.1.2.1.1.5.0"), new OctetString("testname"));
        pduReturn.add(variable);
        
        return pduReturn;
    }
    //create get pdu
    public static PDU createGetPdu(){
        PDU pduReturn = new PDU();
        
        pduReturn.setType(PDU.GET);
        VariableBinding variableBinding = new VariableBinding(new OID("1.3.6.1.2.1.1.5.0"));
        pduReturn.add(variableBinding);
        
        return pduReturn;        
    }
    //send pdu
    public static void sendPdu(PDU pdu, CommunityTarget communityTarget){
        try {
            TransportMapping transportMapping = new DefaultUdpTransportMapping();
            transportMapping.listen();
            Snmp snmp = new Snmp(transportMapping);
            
            ResponseEvent responseEvent = snmp.send(pdu, communityTarget);
            //get reponse pdu
            if(null != responseEvent){
                PDU pduResponse = responseEvent.getResponse();
                System.out.println("size : " + pduResponse.size());
                for(int i = 0; i < pduResponse.size(); i++){
                    VariableBinding variableBinding = pduResponse.get(i);
                    Variable variable = variableBinding.getVariable();
                    System.out.println(variable.toString());
                }
                System.out.println("error:" + pduResponse.getErrorStatusText());
            }
            else{
                System.out.println("timeout ...");
            }
        } catch (IOException e) {
            
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
        
    }
}

 

snmp4j 编程,布布扣,bubuko.com

snmp4j 编程

标签:style   blog   color   java   os   io   for   ar   

原文地址:http://www.cnblogs.com/luckygxf/p/3895909.html

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