2015年5月7日 星期四

Java SNMP Package API Used

Java SNMP Package API 是一個相當容易上手的API。 但作者開發到1.4.2就沒有再往下開發了,我發現了它的timeout機制似乎部會work 如果有興趣的人可以參考作者網站:http://jsevy.com/snmp/ 我所使用的API Sample
 
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;
        }
    }
}
   

CentOS Python 3.7 安裝方式

有些開發的程式在python 3.8的檔案套件不相容,例如pymssql,等。 所以如果不建置虛擬環境的話才特意安裝3.7版本的python 安裝流程如下 # 先進行yum套件的update yum update -y # 安裝相依性套件 yum install gcc o...