📄 snmpconnector.java
字号:
package yww.snmpif;
import java.io.IOException;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Properties;
import java.util.Vector;
import yww.util.Log;
import snmp.SNMPBadValueException;
import snmp.SNMPGetException;
import snmp.SNMPObject;
import snmp.SNMPObjectIdentifier;
import snmp.SNMPSequence;
import snmp.SNMPVarBindList;
import snmp.SNMPv1CommunicationInterface;
/**
* 建立与设备的SNMP连接,获得SNMP信息
* </br>
* @author Administrator
*
*/
public class SnmpConnector {
private SnmpProperty property;
public SnmpConnector()
{
}
public SnmpConnector(SnmpProperty spro)
{
this.property = spro;
}
public SnmpConnector(String ip,String community,int port)
{
property = new SnmpProperty();
property.setIpAddress(ip);
property.setCommunity(community);
property.setPort(port);
}
public SnmpConnector(String ip,String community,int port,int timeOut)
{
property = new SnmpProperty();
property.setIpAddress(ip);
property.setCommunity(community);
property.setPort(port);
property.setTimeout(timeOut);
}
/**
* 获取连接的SNMP属性
* @return SnmpProperty实例,内部结构参考<link>SnmpProperty</link>
*/
public SnmpProperty getSnmpPorperty()
{
return this.property;
}
/**
* 设置连接snmp属性
* @param spro
*/
public void setSnmpProperty(SnmpProperty spro)
{
this.property = spro;
}
/**
* 获得单组绑定列表
* @param label 需要获取数据的OID标识
* @param index 需要获取数据的索引号
* @return 单个绑定列表,是一组OID和值对
*/
public SNMPVarBindList getVarBindList(String label,String index)
{
try {
int version = property.getVersion();
InetAddress address;
address = InetAddress.getByName(property.getIpAddress());
String community = property.getCommunity();
SNMPv1CommunicationInterface comInterface = new SNMPv1CommunicationInterface(version,address,community);
comInterface.setSocketTimeout(property.getTimeout());
String oid = MibParserAPI.getOIDByName(label);
SNMPVarBindList newVars = comInterface.getMIBEntry(oid+"."+index);
return newVars;
} catch (SocketException e) {
Log.debug(this,e);
} catch (UnknownHostException e) {
Log.debug(this,e);
} catch (SNMPGetException e) {
Log.debug(this,e);
} catch (IOException e) {
Log.debug(this,e);
} catch (SNMPBadValueException e) {
Log.debug(this,e);
}
return null;
}
/**
* 获得下一个绑定列表
* @param label 当前OID的标识
* @param index 当前OID的索引
* @return 返回下一个绑定列表
*/
public SNMPVarBindList getNextVarBindList(String label,String index)
{
try {
int version = property.getVersion();
InetAddress address;
address = InetAddress.getByName(property.getIpAddress());
String community = property.getCommunity();
SNMPv1CommunicationInterface comInterface = new SNMPv1CommunicationInterface(version,address,community);
comInterface.setSocketTimeout(property.getTimeout());
String oid = MibParserAPI.getOIDByName(label);
SNMPVarBindList newVars = comInterface.getNextMIBEntry(oid+"."+index);
return newVars;
} catch (SocketException e) {
Log.debug(this,e);
} catch (UnknownHostException e) {
Log.debug(this,e);
} catch (SNMPGetException e) {
Log.debug(this,e);
} catch (IOException e) {
Log.debug(this,e);
} catch (SNMPBadValueException e) {
Log.debug(this,e);
}
return null;
}
/**
* 获得多组绑定列表,一次返回多个数据
* @param labels 需要获取数据的多个OID标识
* @param index 需要获取数据的多个索引
* @return 返回绑定列表,内部有多个OID及值对组成
*/
public SNMPVarBindList getVarBindList(String[] labels,String[] index)
{
try {
int version = property.getVersion();
InetAddress address;
address = InetAddress.getByName(property.getIpAddress());
String community = property.getCommunity();
SNMPv1CommunicationInterface comInterface = new SNMPv1CommunicationInterface(version,address,community);
String[] oids = new String[labels.length];
for(int i=0;i<oids.length;i++)
{
oids[i] = MibParserAPI.getOIDByName(labels[i])+"."+index[i];
}
SNMPVarBindList newVars = comInterface.getMIBEntry(oids);
return newVars;
} catch (SocketException e) {
Log.debug(this,e);
} catch (UnknownHostException e) {
Log.debug(this,e);
} catch (SNMPGetException e) {
Log.debug(this,e);
} catch (IOException e) {
Log.debug(this,e);
} catch (SNMPBadValueException e) {
Log.debug(this,e);
}
return null;
}
/**
* 获取多个结点的下一个结点值,对每个分别取其下一个结点值
* @param labels 结点的标识
* @param index 结点的索引
* @return 返回各个结点的下一个结点的帮定列表
*/
public SNMPVarBindList getNextVarBindList(String[] labels,String[] index)
{
try {
int version = property.getVersion();
InetAddress address;
address = InetAddress.getByName(property.getIpAddress());
String community = property.getCommunity();
SNMPv1CommunicationInterface comInterface = new SNMPv1CommunicationInterface(version,address,community);
String[] oids = new String[labels.length];
for(int i=0;i<oids.length;i++)
{
oids[i] = MibParserAPI.getOIDByName(labels[i])+"."+index[i];
}
SNMPVarBindList newVars = comInterface.getNextMIBEntry(oids);
return newVars;
} catch (SocketException e) {
Log.debug(this,e);
} catch (UnknownHostException e) {
Log.debug(this,e);
} catch (SNMPGetException e) {
Log.debug(this,e);
} catch (IOException e) {
Log.debug(this,e);
} catch (SNMPBadValueException e) {
Log.debug(this,e);
}
return null;
}
/**
* 获取单个结点值
* @param label 单个结点的OID标识
* @param index 单个结点的索引
* @return 单个结点的OID值
*/
public String get(String label,String index)
{
SNMPVarBindList vars = getVarBindList(label,index);
SNMPSequence pair = (SNMPSequence)(vars.getSNMPObjectAt(0));
if(pair == null||pair.size()==0)
{
return null;
}
// SNMPObjectIdentifier snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0);
SNMPObject snmpValue = pair.getSNMPObjectAt(1);
if(snmpValue==null||snmpValue.equals(""))
return null;
else
return snmpValue.toString();
}
/**
* 获取单个结点的值,不带索引的情况,使用默认索引--0
* @param label 单个结点标识
* @return 返回单个结点的值
*/
public String get(String label)
{
return get(label,"0");
}
/**
* 获取多个结点,结点标识和结点索引需要一一对应
* @param labels 多个结点的标识
* @param indeies 多个结点的索引,与标识的顺序一致
* @return 返回多个值,以属性对形式一一对应,例:sysName=abc.
*/
public Properties gets(String[] labels,String[] indeies)
{
Properties pro = new Properties();
SNMPVarBindList vars = getVarBindList(labels,indeies);
if(vars==null||vars.size()!=labels.length)
{
Log.error("can not get value with labels and indeies");
return null;
}
for(int i=0;i<vars.size();i++)
{
SNMPSequence pair = (SNMPSequence)vars.getSNMPObjectAt(i);
// SNMPObjectIdentifier snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0);
SNMPObject snmpValue = pair.getSNMPObjectAt(1);
pro.put(labels[i],snmpValue);
}
return pro;
}
/**
* 获取多个结点,不带索引,即使用默认索引"0"的情况,
* @param labels 多个结点标识
* @return 返回多个值的属性对
*/
public Properties gets(String[] labels)
{
String[] indeies = new String[labels.length];
for(int i=0;i<indeies.length;i++)
{
indeies[i]="0";
}
return gets(labels,indeies);
}
/**
* 获取下一个结点的值
* @param label 当前结点的标识
* @param index 当前结点的索引
* @return 下一个结点的值
*/
public String getNext(String label,String index)
{
SNMPVarBindList vars = getNextVarBindList(label,index);
SNMPSequence pair = (SNMPSequence)(vars.getSNMPObjectAt(0));
if(pair == null||pair.size()==0)
{
return null;
}
// SNMPObjectIdentifier snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0);
SNMPObject snmpValue = pair.getSNMPObjectAt(1);
if(snmpValue==null||snmpValue.equals(""))
return null;
else
return snmpValue.toString();
}
/**
* 获取下一个结点的值,使用默认方式,索引为0
* @param label 当前结点的标识
* @return 下一个结点的值
*/
public String getNext(String label)
{
return getNext(label,"0");
}
/**
* 获取当前的结点的下多个结点的值,
* @param labels 当前结点的多个标识
* @param indeies 当前结点的多个索引
* @return 多个结点的下一个值,
*/
public Properties getsNext(String[] labels,String[] indeies)
{
Properties pro = new Properties();
SNMPVarBindList vars = getNextVarBindList(labels,indeies);
if(vars==null||vars.size()!=labels.length)
{
Log.error("can not get value with labels and indeies");
return null;
}
for(int i=0;i<vars.size();i++)
{
SNMPSequence pair = (SNMPSequence)vars.getSNMPObjectAt(i);
// SNMPObjectIdentifier snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0);
SNMPObject snmpValue = pair.getSNMPObjectAt(1);
pro.put(labels[i],snmpValue);
}
return pro;
}
/**
* 获取默认情况下多个结点的下一个值,以索引号0结尾
* @param labels 当前多个结点的标识
* @return 多个下一个结点的属性对
*/
public Properties getsNext(String[] labels)
{
String[] indeies = new String[labels.length];
for(int i=0;i<indeies.length;i++)
{
indeies[i]="0";
}
return getsNext(labels,indeies);
}
/**
* 获取该子树下所有的结点,适用于知道部分索引的情况
* @param label 结点标识
* @param index 部分索引
* @return String的二元数组,第一部分索引,第二部分结点值
*/
public Vector[] snmpWalk(String label,String index)
{
String oid = MibParserAPI.getOIDByName(label)+"."+index;
Log.debug("oid is"+oid);
String nextOid = oid;
Vector[] result = new Vector[2];
result[0] = new Vector();
result[1] = new Vector();
try {
int version = property.getVersion();
InetAddress address;
address = InetAddress.getByName(property.getIpAddress());
String community = property.getCommunity();
SNMPv1CommunicationInterface comInterface = new SNMPv1CommunicationInterface(version,address,community);
comInterface.setSocketTimeout(property.getTimeout());
SNMPVarBindList vars = comInterface.getNextMIBEntry(nextOid);
while(vars!=null)
{
Log.debug("vars = :"+vars);
SNMPSequence pair = (SNMPSequence)vars.getSNMPObjectAt(0);
Log.debug(pair.toString());
Log.debug(String.valueOf(pair.size()));
if(pair!=null&&pair.size()==2)
{
SNMPObjectIdentifier snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0);
SNMPObject snmpValue = pair.getSNMPObjectAt(1);
Log.debug("snmpOID="+snmpOID);
Log.debug("snmpValue="+snmpValue);
if(snmpOID!=null&&snmpValue!=null)
{
nextOid = snmpOID.toString();
if(nextOid.indexOf(oid)!=-1)
{
result[0].add(nextOid.substring(oid.length()));
result[1].add(snmpValue.toString());
}else
break;
}else
break;
}else
break;
vars = comInterface.getNextMIBEntry(nextOid);
}
return result;
} catch (SocketException e) {
Log.debug(this,e);
} catch (UnknownHostException e) {
Log.debug(this,e);
} catch (SNMPGetException e) {
Log.debug(this,e);
} catch (IOException e) {
Log.debug(this,e);
} catch (SNMPBadValueException e) {
Log.debug(this,e);
}
return null;
}
/**
* 获取该结点下所有的结点
* @param label 结点标识
* @return 索引与结点值的组对,Vector[0]为索引列表,Vector[1]为结点值列表
*/
public Vector[] snmpWalk(String label)
{
String oid = MibParserAPI.getOIDByName(label);
Log.debug("oid is"+oid);
String nextOid = oid;
Vector[] result = new Vector[2];
result[0] = new Vector();
result[1] = new Vector();
try {
int version = property.getVersion();
InetAddress address;
address = InetAddress.getByName(property.getIpAddress());
String community = property.getCommunity();
SNMPv1CommunicationInterface comInterface = new SNMPv1CommunicationInterface(version,address,community);
comInterface.setSocketTimeout(property.getTimeout());
SNMPVarBindList vars = comInterface.getNextMIBEntry(nextOid);
while(vars!=null)
{
Log.debug("vars = :"+vars);
SNMPSequence pair = (SNMPSequence)vars.getSNMPObjectAt(0);
Log.debug(pair.toString());
Log.debug(String.valueOf(pair.size()));
if(pair!=null&&pair.size()==2)
{
SNMPObjectIdentifier snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0);
SNMPObject snmpValue = pair.getSNMPObjectAt(1);
Log.debug("snmpOID="+snmpOID);
Log.debug("snmpValue="+snmpValue);
if(snmpOID!=null&&snmpValue!=null)
{
nextOid = snmpOID.toString();
if(nextOid.indexOf(oid)!=-1)
{
result[0].add(nextOid.substring(oid.length()));
result[1].add(snmpValue.toString());
}else
break;
}else
break;
}else
break;
vars = comInterface.getNextMIBEntry(nextOid);
}
return result;
} catch (SocketException e) {
Log.debug(this,e);
} catch (UnknownHostException e) {
Log.debug(this,e);
} catch (SNMPGetException e) {
Log.debug(this,e);
} catch (IOException e) {
Log.debug(this,e);
} catch (SNMPBadValueException e) {
Log.debug(this,e);
}
return null;
}
public Properties[] getTable(String tableEntry)
{
return null;
}
public static void main(String[] args)
{
Log.debug("dddddddd");
while(true)
{
;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -