Beispiel #1
0
class SNMP_Service():
    def __init__(self, hostname, **kwargs):
        self.switch = hostname
        self.version = kwargs.get('version', 2)
        if self.version < 3:
            self.community = kwargs.get('community', 'public')
            self.session = Session(hostname=self.switch, community=self.community, version=self.version)
        else:
            self.security_level = kwargs.get('security_level', 'authNoPriv')
            self.security_username = kwargs.get('security_username', 'opro')
            self.auth_protocol = kwargs.get('auth_protocol', 'SHA')
            self.auth_password = kwargs.get('auth_password', '')
            self.privacy_protocol = kwargs.get('privacy_protocol', 'aes')
            self.privacy_password = kwargs.get('privacy_password', '')
            self.session = Session(hostname=self.switch,
                                   security_level=self.security_level,
                                   security_username=self.security_username,
                                   auth_protocol=self.auth_protocol,
                                   auth_password=self.auth_password,
                                   privacy_protocol=self.privacy_protocol,
                                   privacy_password=self.privacy_password,
                                   version=self.version)

    def sys_descr(self):
        d = self.session.get('sysDescr.0')
        [descr, fw, rom] = d.value.split(',')
        return (self.switch, descr, fw, rom)

    def num_ports(self):
        d = self.session.get('')
        [descr, fw, rom] = d.value.split(',')

    def model(self):
        d = self.session.get('ENTITY-MIB::entPhysicalModelName.1')
        return d

    def firmware(self):
        d = self.session.get('ENTITY-MIB::entPhysicalSoftwareRev.1')
        return d

    def get(self, oid):
        return self.session.get(oid)

    def getfirst(self, oid):
        ret = self.session.get_next(oid)
        while ret is not None and not len(ret.value):
            ret = self.session.get_next(ret.oid)
        return ret

    def getall(self, oid, filter_by_value=False):
        ret = self.session.walk(oid)
        if filter_by_value:
            return [lambda x: x.value for x in ret]
        return ret

    def set(self, oid, value, snmp_type=None):
        return self.session.set(oid, value, snmp_type)

    def set_multiple(self, oids):
        return self.session.set_multiple(oids)
def setValue(oids, ip_address, community):
    session = Session(hostname=ip_address,
                      community=community,
                      version=2,
                      use_numeric=True)
    try:
        output = session.set_multiple(oids)
    except Exception as e:
        print("setValue: ip_address: " + ip_address, e)
from easysnmp import Session
from easysnmp import exceptions as SNMPexceptions

ip = "1.1.1.1"
session = Session(hostname=ip, community='public', version=2)
interfaces = []

device_interfaces = session.walk('.1.3.6.1.2.1.2.2.1.2')
for inter in device_interfaces:
    if inter.value == "Null0":
        continue
    oid = ".1.3.6.1.2.1.2.2.1.7.{}".format(inter.oid_index)
    interfaces.insert(0, (oid, 2))

try:
    session.set_multiple(interfaces)
except SNMPexceptions.EasySNMPTimeoutError:
    print "killed some interfaces"

#Only seems to work for interfaces that are populated/active(not an issue)
Beispiel #4
0
class Device():
    def __init__(self, IPaddress, Port, Community, Version, Timeout):
        self.dev = Session(hostname=IPaddress,
                           remote_port=Port,
                           community=Community,
                           version=Version,
                           timeout=Timeout)

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ READ METHODS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
    #Method to numeric variable from regular OID
    def read_num(self, VarNameList, OIDList, MultiplierList):
        # Arguments:
        # VarNameList       :   list of variable name
        # OIDList           :   list of variable OID address
        # MultiplierList    :   list of multiplier
        # Return            :   dictionary of variable name and its value

        self.values = []

        self.operation = math.ceil(len(OIDList) / dataPerAccess)
        i = 0
        for opr in range(self.operation):
            try:
                data = self.dev.get(OIDList[i:i + dataPerAccess])
            except:
                data = self.dev.get(OIDList[i:])

            for item in data:
                self.values.append(int(item.value))

            i += dataPerAccess

        for i in range(0, len(self.values)):
            self.values[i] = round(self.values[i] * MultiplierList[i], 3)

        self.Result = dict(zip(VarNameList, self.values))
        return self.Result

    #Method to string variable from regular OID
    def read_string(self, VarNameList, OIDList):
        # Arguments:
        # VarNameList       :   list of variable name
        # OIDList           :   list of variable OID address
        # Return            :   dictionary of variable name and its value

        self.values = []

        self.operation = math.ceil(len(OIDList) / dataPerAccess)
        i = 0
        for opr in range(self.operation):
            try:
                data = self.dev.get(OIDList[i:i + dataPerAccess])
            except:
                data = self.dev.get(OIDList[i:])

            for item in data:
                self.values.append(str(item.value))

            i += dataPerAccess

        self.Result = dict(zip(VarNameList, self.values))
        return self.Result

    #Method to string variable from Table OID
    def read_num_tab(self, VarNameList, OIDList, rowList, MultiplierList):
        # Arguments:
        # VarNameList       :   list of variable name
        # OIDList           :   list of variable OID address
        # rowList           :   list of total row that we want to get from table column (max repetition in getBulk operation)
        # MultiplierList    :   list of multiplier
        # Return            :   dictionary of variable name and its value

        self.values = []
        self.Result = {}
        i = 0
        for oid in OIDList:
            data = self.dev.get_bulk(oid, max_repetitions=rowList[i])

            j = 1
            for item in data:
                self.Result[VarNameList[i] + "_%d" % j] = round(
                    int(item.value) * MultiplierList[i], 3)
                j += 1
            i += 1

        return self.Result

    #Method to string variable from Table OID
    def read_string_tab(self, VarNameList, OIDList, rowList):
        # Arguments:
        # VarNameList       :   list of variable name
        # OIDList           :   list of variable OID address
        # rowList           :   list of total row that we want to get from table column (max repetition in getBulk operation)
        # Return            :   dictionary of variable name and its value

        self.values = []
        self.Result = {}
        i = 0
        for oid in OIDList:
            data = self.dev.get_bulk(oid, max_repetitions=rowList[i])

            j = 1
            for item in data:
                self.Result[VarNameList[i] + "_%d" % j] = str(item.value)
                j += 1
            i += 1

        return self.Result

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ WRITE METHODS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
    # Method to write a value in single OID
    def write(self, OID, value, snmp_type=None):
        # Arguments:
        # OID       :   object identifier of a variable
        # value     :   value that you want to write in the OID
        # snmp_type :   data type in object
        #

        if snmp_type == None:
            self.dev.set(registerAddress, value)
        else:
            self.dev.set(registerAddress, value, snmp_type)

    # Method to write values in multiple OIDs
    def write_multiple(self, OIDList, valueList, snmp_type=None):
        # Arguments:
        # OIDList   :   list of object identifier
        # valueList :   list of value

        if snmp_type == None:
            oid_val = list(zip(OIDList, valueList))
            self.dev.set_multiple(oid_val)
        else:
            oid_val = list(zip(OIDList, valueList, snmp_type))
            self.dev.set_multiple(oid_val)
Beispiel #5
0
class SNMP_Service():
    def __init__(self, hostname, **kwargs):
        self.switch = hostname
        self.version = kwargs.get('version', 2)
        if self.version < 3:
            self.community = kwargs.get('community', 'public')
            self.session = Session(hostname=self.switch,
                                   community=self.community,
                                   version=self.version)
        else:
            self.security_level = kwargs.get('security_level', 'authNoPriv')
            self.security_username = kwargs.get('security_username', 'opro')
            self.auth_protocol = kwargs.get('auth_protocol', 'SHA')
            self.auth_password = kwargs.get('auth_password', '')
            self.privacy_protocol = kwargs.get('privacy_protocol', 'aes')
            self.privacy_password = kwargs.get('privacy_password', '')
            self.session = Session(hostname=self.switch,
                                   security_level=self.security_level,
                                   security_username=self.security_username,
                                   auth_protocol=self.auth_protocol,
                                   auth_password=self.auth_password,
                                   privacy_protocol=self.privacy_protocol,
                                   privacy_password=self.privacy_password,
                                   version=self.version)

    def sys_descr(self):
        d = self.session.get('sysDescr.0')
        [descr, fw, rom] = d.value.split(',')
        return (self.switch, descr, fw, rom)

    def num_ports(self):
        d = self.session.get('')
        [descr, fw, rom] = d.value.split(',')

    def model(self):
        d = self.session.get('ENTITY-MIB::entPhysicalModelName.1')
        return d

    def firmware(self):
        d = self.session.get('ENTITY-MIB::entPhysicalSoftwareRev.1')
        return d

    def get(self, oid):
        return self.session.get(oid)

    def getfirst(self, oid):
        ret = self.session.get_next(oid)
        while ret is not None and not len(ret.value):
            ret = self.session.get_next(ret.oid)
        return ret

    def getall(self, oid, filter_by_value=False):
        ret = self.session.walk(oid)
        if filter_by_value:
            return [lambda x: x.value for x in ret]
        return ret

    def set(self, oid, value, snmp_type=None):
        return self.session.set(oid, value, snmp_type)

    def set_multiple(self, oids):
        return self.session.set_multiple(oids)