import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.logging.Level; import java.util.logging.Logger; import snmp.SNMPBadValueException; import snmp.SNMPGetException; import snmp.SNMPObject; import snmp.SNMPObjectIdentifier; import snmp.SNMPOctetString; import snmp.SNMPSequence; import snmp.SNMPVarBindList; import snmp.SNMPv1CommunicationInterface; /** * * @author Derek.Li */ public class SnmpGet { public static void println(int level, String context) { if (level > 0) { return; } System.out.println(context); } // public final String itemID = "1.3.6.1.2.1.1.1.0"; public String snmpGet(String IP, String community, String OID) { try { InetAddress hostAddress = InetAddress.getByName(IP); int version = 0; //設定0的時候為使用snmpv1 SNMPv1CommunicationInterface comInterface = new SNMPv1CommunicationInterface(version, hostAddress, community); comInterface.setSocketTimeout(5000); //ms = 1/1000 s // the getMIBEntry method of the communications interface returns an SNMPVarBindList // object; this is essentially a Vector of SNMP (OID,value) pairs. In this case, the // returned Vector has just one pair inside it. SNMPVarBindList newVars = comInterface.getMIBEntry(OID); // extract the (OID,value) pair from the SNMPVarBindList; the pair is just a two-element // SNMPSequence SNMPSequence pair = (SNMPSequence) (newVars.getSNMPObjectAt(0)); // extract the object identifier from the pair; it's the first element in the sequence SNMPObjectIdentifier snmpOID = (SNMPObjectIdentifier) pair.getSNMPObjectAt(0); // extract the corresponding value from the pair; it's the second element in the sequence SNMPObject snmpValue = pair.getSNMPObjectAt(1); // print out the String representation of the retrieved value String result = snmpValue.toString(); if(OID.equals("1.3.6.1.2.1.2.2.1.6.2")) return ((SNMPOctetString)snmpValue).toHexString(); return result; } catch (Exception e) { String result = null; return result; } } }
2015年5月7日 星期四
Java SNMP Package API Used
Java SNMP Package API 是一個相當容易上手的API。
但作者開發到1.4.2就沒有再往下開發了,我發現了它的timeout機制似乎部會work
如果有興趣的人可以參考作者網站:http://jsevy.com/snmp/
我所使用的API Sample
訂閱:
文章 (Atom)
CentOS Python 3.7 安裝方式
有些開發的程式在python 3.8的檔案套件不相容,例如pymssql,等。 所以如果不建置虛擬環境的話才特意安裝3.7版本的python 安裝流程如下 # 先進行yum套件的update yum update -y # 安裝相依性套件 yum install gcc o...
-
當我們要對SQL中進行運算,如MAX、MIN、SUM,等等。 而當預計算的欄位是NVARCHAR或VARCHAR就會出現類似以下錯誤訊息。 sum 運算子的運算元資料類型 nvarchar 無效。 調整SQL語法如下: SELECT c1,c2,c3,sum(c4)...
-
之前在開發Java監控Server需要用到snmp套件是使用snmp4j 在.net(C#)是使用SNMPSharpNet 今天則是記錄Python上找了一下比較方便的套件pysnmp 官方網站如下: http://snmplabs.com/pysnmp/index.htm...
-
原本有些API在應用的時候,原本的想法就是透過Telnet的方式來呼叫進行互動式的操作。 但為了簡化環境的因子,想說如果可以利用C#呼叫外部程式的方式,進行溝通鳩可以了。 雖然事與願違,但還是將測試的過程進行了以下的紀錄。 Process process = new ...