Snmp获取OID信息v1/v2/v3都行_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Snmp获取OID信息v1/v2/v3都行

Snmp获取OID信息v1/v2/v3都行

 2011/11/29 10:14:30  yangyangmyself  http://yangyangmyself.iteye.com  我要评论(1)
  • 摘要:packagecom.shine.framework.core.util;importjava.util.List;importorg.snmp4j.CommunityTarget;importorg.snmp4j.PDU;importorg.snmp4j.ScopedPDU;importorg.snmp4j.Snmp;importorg.snmp4j.TransportMapping;importorg.snmp4j.UserTarget;importorg.snmp4j.event
  • 标签:snmp
class="java">
package com.shine.framework.core.util;

import java.util.List;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.ScopedPDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.UserTarget;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.MPv3;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.security.AuthMD5;
import org.snmp4j.security.PrivDES;
import org.snmp4j.security.SecurityLevel;
import org.snmp4j.security.SecurityModels;
import org.snmp4j.security.SecurityProtocols;
import org.snmp4j.security.USM;
import org.snmp4j.security.UsmUser;
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 SnmpHelper4j extends SnmpAbstract {
	//Snmp Instance
	private Snmp snmp=null;
	private String ip;
	//unit(共同体)
	private String community;
	private int port = 161;
	//Snmp version(v1=0;v2=1;v3=3)
	private int version = 0;
	private TransportMapping tm;
	private Address targetAddress;
	//Snmp Defualt Use UDP Protocl(You can you TCP Protocl)
	private String protol="udp";
	
	public SnmpHelper4j(){
		
	}
	public SnmpHelper4j(String ip, String community) throws Exception{
		
		this.init(ip, community);
	}
	public SnmpHelper4j(String ip, String community, int port) throws Exception{
		
		this.init(ip, community, port);
	}
	/**
	 * 动态监听端口
	 */
	@Override
	public void init(String ip, String community, int port) throws Exception {
		this.ip=ip;
		this.community=community;
		StringBuffer tstr = new StringBuffer(this.protol).append(":").append(ip).append("/").append(port);
		targetAddress = GenericAddress.parse(tstr.toString()); 
		tm = new DefaultUdpTransportMapping();
		snmp = new Snmp(tm);
	}
	
	/**
	 * 默认监听端口161
	 */
	@Override
	public void init(String ip, String community) throws Exception {
		this.ip=ip;
		this.community=community;
		StringBuffer tstr = new StringBuffer(this.protol).append(":").append(ip).append("/").append(port);
		targetAddress = GenericAddress.parse(tstr.toString()); 
		tm = new DefaultUdpTransportMapping();
		snmp = new Snmp(tm);		
	}

	@Override
	public String getOidValueString(String oid){
		for(int i=0;i<=3;i++){
			if(i==0 ){
				String result = getV1OidValueString(oid,0);
				if(result!=null){
					return result;
				}
			}
			if(i==1){
				String result = getV1OidValueString(oid,i);
				if(result!=null){
					return result;
				}
			}
			if(i==3){
				String result = getV3OidValueString(oid);
				if(result!=null){
					return result;
				}
			}
		}
		return null;
	}
	/**
	 * 获取V1/V2 OID	值
	 * @param oid
	 * @param snmpv
	 * @return
	 */
	public String getV1OidValueString(String oid,int snmpv){
		this.state = true;
		CommunityTarget target = new CommunityTarget();
		target.setCommunity(new OctetString(this.community));
		target.setAddress(this.targetAddress);
		target.setVersion(snmpv);
		target.setTimeout(100);
		target.setRetries(1);
	
		PDU pdu = new PDU();
		pdu.add(new VariableBinding(new OID(oid)));
		pdu.setType(PDU.GET);			
        try{
 			tm.listen();
 			ResponseEvent response = snmp.send(pdu, target);
 			if (response.getResponse() != null) {
 			    String statuStr = response.getResponse().getErrorStatusText();
 			    if("Success".equals(statuStr)){
 			    	this.version=snmpv;
 			    	return response.getResponse().toString()+" --->Snmpv"+(this.version+1)+" IP:"+this.ip; 
 			    }
 			}else{
 				return null;
 			}
 		}catch(Exception e){
 			e.printStackTrace();
 		}finally{
 			this.state = false;
 			close();
 			pdu=null;
 			target=null;
 		}
 		return null;
	}
	/**
	 * 获取V3 OID值
	 * @param oid
	 * @param snmpv
	 * @return
	 */
	public String getV3OidValueString(String oid){
		this.state = true;
		USM usm = new USM(SecurityProtocols.getInstance(),new OctetString(MPv3.createLocalEngineID()), 0);
		SecurityModels.getInstance().addSecurityModel(usm);

		ScopedPDU spdu = new ScopedPDU();
		spdu.add(new VariableBinding(new OID(oid)));
		spdu.setType(ScopedPDU.GET);
		spdu.setMaxRepetitions(50);
		UserTarget target = new UserTarget();
		target.setAddress(this.targetAddress);
		target.setRetries(1);
		target.setTimeout(500);
		target.setVersion(SnmpConstants.version3);
		target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
		target.setSecurityName(new OctetString("MD5DES"));
		snmp.getUSM().addUser(new OctetString("MD5DES"), new UsmUser(new OctetString("MD5DES"), AuthMD5.ID,new OctetString("MD5DESUserAuthPassword"), PrivDES.ID,new OctetString("MD5DESUserPrivPassword")));		
		try{
			snmp.listen();
			ResponseEvent response = snmp.send(spdu, target);
			if (response.getResponse() != null) {
			    String statuStr = response.getResponse().getErrorStatusText();
 			    if("Success".equals(statuStr)){
 			    	this.version=3;
 			    	return response.getResponse().toString(); 
 			    } 
			}else{
				return null;
			}	
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			this.state = false;
			close();
			target=null;
			spdu = null;
			usm = null;
		}
 		return null;
	}	

	@Override
	public void reconnection(String ip, String community, int port) {
		try {
			if (tm != null)
				tm.close();
			this.community=community;
			StringBuffer tstr = new StringBuffer(this.protol).append(":").append(ip).append("/").append(port);
			targetAddress = GenericAddress.parse(tstr.toString()); 
			tm = new DefaultUdpTransportMapping();
			snmp = new Snmp(tm);	
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void reconnection() {
		try {
			if (tm != null)
				tm.close();
			StringBuffer tstr = new StringBuffer(this.protol).append(":").append(this.ip).append("/").append(this.port);
			targetAddress = GenericAddress.parse(tstr.toString()); 
			tm = new DefaultUdpTransportMapping();
			snmp = new Snmp(tm);	
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 关闭监听
	 */
	@Override
	public void close() {
		try{
			if(tm != null){
				tm.close();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {	
		SnmpHelper4j sh = new SnmpHelper4j("192.168.2.18", "public", 161);
		System.out.println(sh.getOidValueString("1.3.6.1.2.1.1.1.0"));
	}
}

    网友 2014/3/24 10:46:20 发表

    楼主,Action继承的SnmpAbstract,这个类没有放出来呀

发表评论
用户名: 匿名