SNMP請到 http://gicl.cs.drexel.edu/people/sevy/snmp/
下載支援SNMP的API
我使用的是Version 1.4.2
以下使用JAVA IDE Eclipse來實驗
將SNMP的source匯入專案
1. 解壓縮剛下載的source_1.4.2.rar
2. 將surce資料夾下的snmp資料夾拖拉到專案例如SNMPTest上
3. Eclipse會自己匯入專案 (注意 : 專案內請不要有同名資料夾例如SNMP資料夾 Eclipse會自己產生)
程式範例:
import snmp.*; import java.net.*; public class SnmpTest { public static void main(String[] args) { try { InetAddress hostAddress = InetAddress.getByName("遠端支援SNMP的IP位址"); String community = "public"; int version = 0; // SNMPv1 SNMPv1CommunicationInterface comInterface = new SNMPv1CommunicationInterface(version, hostAddress, community); String itemID = "1.3.6.1.2.1.1.1.0"; System.out.println("Retrieving value corresponding to OID " + itemID); SNMPVarBindList newVars = comInterface.getMIBEntry(itemID); SNMPSequence pair = (SNMPSequence)(newVars.getSNMPObjectAt(0)); SNMPObject snmpValue = pair.getSNMPObjectAt(1); System.out.println("Retrieved value: type " + snmpValue.getClass().getName() + ", value " + snmpValue.toString()); itemID = "1.3.6.1.2.1.1.3.0"; System.out.println("Retrieving value corresponding to OID " + itemID); newVars = comInterface.getMIBEntry(itemID); pair = (SNMPSequence)(newVars.getSNMPObjectAt(0)); snmpValue = pair.getSNMPObjectAt(1); System.out.println("Retrieved value: type " + snmpValue.getClass().getName() + ", value " + snmpValue.toString()); } catch(Exception e) { System.out.println("Exception during SNMP operation: " + e + "\n"); } } }
沒有留言:
張貼留言