⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mibparserapi.java

📁 纯java的SNMPv1/v2协议实现,并在其基础上进行封装以便于使用.
💻 JAVA
字号:
package yww.snmpif;

import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;

import net.percederberg.mibble.Mib;
import net.percederberg.mibble.MibLoader;
import net.percederberg.mibble.MibLoaderException;
import net.percederberg.mibble.MibSymbol;
import net.percederberg.mibble.MibValue;
import net.percederberg.mibble.MibValueSymbol;
import net.percederberg.mibble.value.ObjectIdentifierValue;
import yww.util.Log;

/**
 * 用户加载指定的MIB文件,从中根据name获得oid
 * <br>1.文件加载
 * <br>2.name和oid的转换
 * @author Administrator
 *
 */
public class MibParserAPI {

	static MibLoader loader = new MibLoader();;
	static Hashtable mibs = new Hashtable();
	
	
	/**
	 * 单个载入MIB库
	 * @param path - the path of mib file
	 */ 
	public static void load(String path)
	{
		try{
			File file = new File(path);
			Log.info("loading "+file);
			loader.addDir(file.getParentFile());
			
			Mib m = loader.load(file);
			Log.info("loaded "+m);
			if(m!=null) mibs.put(m.getName(),m);
			else 
				Log.error("can not load "+path);
		}catch(IOException e)
		{
			Log.error("io error",e);
		}catch(MibLoaderException e){
			Log.error("load error",e);
		}
	}
	
	/**
	 * 载入多个MIB库
	 * @param path - the String array to hold mib file paths
	 */
	public static void load(String[] path)
	{
		for(int i=0;i<path.length;i++)
		{
			load(path[i]);
		}
	}
	
	/**
	 * 根据mib中定义的oid名称获取OID值
	 * @param name - the name defined in mib
	 * @return the oid correspond to name
	 */
	public static String getOIDByName(String name)
	{
		Enumeration enu = mibs.keys();
		while(enu.hasMoreElements())
		{
			String mibName = (String)enu.nextElement();
			String oid = getOIDByName(mibName,name);
			if(oid!=null) return oid;
		}
		return null;
	}
	
	/**
	 * 在指定mib库文件时获取与oid名称对应的OID值
	 * @param mibName - mib file name,exclude path,just a file name
	 * @param name - the name defined in mib
	 * @return the oid correspond to name in specified mib file
	 */
	public static String getOIDByName(String mibName,String name)
	{
		Mib mib = (Mib)mibs.get(mibName);
		MibSymbol symbol = mib.getSymbol(name);
		if(symbol instanceof MibValueSymbol)
		{
			MibValue value = ((MibValueSymbol)symbol).getValue();
			if(value instanceof ObjectIdentifierValue){
				ObjectIdentifierValue oid = (ObjectIdentifierValue)value;
				return oid.toString();
			}else return null;
		}else return null;
	}
	
	/**
	 * 根据OID值获得相应的oid名称
	 * @param OID - oid value,
	 * @return oid name correspond to oid value
	 */
	public static String getNameByOID(String OID)
	{
		Enumeration enu = mibs.keys();
		while(enu.hasMoreElements())
		{
			String mibName = (String)enu.nextElement();
			String name = getNameByOID(mibName,OID);
			if(name!=null) return name;
		}
		return null;
	}
	
	/**
	 * 在指定的MIB文件中获取OID对应的名称
	 * @param mibName - mib file name,exclude path,just a file name
	 * @param OID - oid vlaue
	 * @return the oid name correspond to oid value in specified mib file
	 */
	public static String getNameByOID(String mibName,String OID)
	{
		Mib mib = (Mib)mibs.get(mibName);
		MibSymbol symbol = mib.getSymbolByOid(OID);
		if(symbol!=null)
		{
			String name  = symbol.getName();
			return name;
		}else
			return null;
	}
	
	public static void main(String[] args)
	{
		MibParserAPI.load("D:\\eclipse-SDK-3.1-win32\\eclipse\\workspace\\mibble\\mibs\\ietf\\RFC1213-MIB");
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -