?? 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 需要獲取數(shù)據(jù)的OID標識
* @param index 需要獲取數(shù)據(jù)的索引號
* @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;
}
/**
* 獲得多組綁定列表,一次返回多個數(shù)據(jù)
* @param labels 需要獲取數(shù)據(jù)的多個OID標識
* @param index 需要獲取數(shù)據(jù)的多個索引
* @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的二元數(shù)組,第一部分索引,第二部分結點值
*/
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 + -