Ejemplo n.º 1
0
    def get_auth_data(instance):
        """
        Generate a Security Parameters object based on the instance's
        configuration.
        See http://pysnmp.sourceforge.net/docs/current/security-configuration.html
        """
        if 'community_string' in instance:
            # SNMP v1 - SNMP v2

            # See http://pysnmp.sourceforge.net/docs/current/security-configuration.html
            if int(instance.get('snmp_version', 2)) == 1:
                return hlapi.CommunityData(instance['community_string'],
                                           mpModel=0)
            return hlapi.CommunityData(instance['community_string'], mpModel=1)

        elif 'user' in instance:
            # SNMP v3
            user = instance['user']
            auth_key = None
            priv_key = None
            auth_protocol = None
            priv_protocol = None
            if 'authKey' in instance:
                auth_key = instance['authKey']
                auth_protocol = hlapi.usmHMACMD5AuthProtocol
            if 'privKey' in instance:
                priv_key = instance['privKey']
                auth_protocol = hlapi.usmHMACMD5AuthProtocol
                priv_protocol = hlapi.usmDESPrivProtocol
            if 'authProtocol' in instance:
                auth_protocol = getattr(hlapi, instance['authProtocol'])
            if 'privProtocol' in instance:
                priv_protocol = getattr(hlapi, instance['privProtocol'])
            return hlapi.UsmUserData(user, auth_key, priv_key, auth_protocol,
                                     priv_protocol)
        else:
            raise ConfigurationError(
                'An authentication method needs to be provided')
Ejemplo n.º 2
0
    def get_value(self, oid):
        errorIndication, errorStatus, errorIndex, varBinds = next(
            hlapi.getCmd(
                hlapi.SnmpEngine(),
                hlapi.UsmUserData(userName=self.username,
                                  authKey=self.authKey,
                                  privKey=self.privKey,
                                  authProtocol=self.authProtocol,
                                  privProtocol=self.privProtocol),
                hlapi.UdpTransportTarget(
                    (self.host, 161)), hlapi.ContextData(),
                hlapi.ObjectType(hlapi.ObjectIdentity(oid))))

        if errorIndication:
            print(errorIndication)
        elif errorStatus:
            print('%s at %s' %
                  (errorStatus.prettyPrint(),
                   errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
        else:
            for varBind in varBinds:
                print(varBind)
                print(' = '.join([x.prettyPrint() for x in varBind]))
Ejemplo n.º 3
0
    def get_auth_data(cls, instance):
        '''
        Generate a Security Parameters object based on the instance's
        configuration.
        See http://pysnmp.sourceforge.net/docs/current/security-configuration.html
        '''
        if "community_string" in instance:
            # SNMP v1 - SNMP v2

            # See http://pysnmp.sourceforge.net/docs/current/security-configuration.html
            if int(instance.get("snmp_version", 2)) == 1:
                return hlapi.CommunityData(instance['community_string'],
                                           mpModel=0)
            return hlapi.CommunityData(instance['community_string'], mpModel=1)

        elif "user" in instance:
            # SNMP v3
            user = instance["user"]
            auth_key = None
            priv_key = None
            auth_protocol = None
            priv_protocol = None
            if "authKey" in instance:
                auth_key = instance["authKey"]
                auth_protocol = hlapi.usmHMACMD5AuthProtocol
            if "privKey" in instance:
                priv_key = instance["privKey"]
                auth_protocol = hlapi.usmHMACMD5AuthProtocol
                priv_protocol = hlapi.usmDESPrivProtocol
            if "authProtocol" in instance:
                auth_protocol = getattr(hlapi, instance["authProtocol"])
            if "privProtocol" in instance:
                priv_protocol = getattr(hlapi, instance["privProtocol"])
            return hlapi.UsmUserData(user, auth_key, priv_key, auth_protocol,
                                     priv_protocol)
        else:
            raise Exception("An authentication method needs to be provided")
Ejemplo n.º 4
0
from pysnmp import hlapi
from pysnmp.hlapi import *
from pysnmp.hlapi import SnmpEngine
import code
import itertools
import time
from pysnmp.smi import builder

# SNMPv3 alternative
'''
hlapi.UsmUserData('testuser', authKey='authenticationkey', privKey='encryptionkey', authProtocol=hlapi.usmHMACSHAAuthProtocol, privProtocol=hlapi.usmAesCfb128Protocol)
'''

"""
The aim of this file is to provide a bunch of methods as tools for SNMP operations.
What is called MIB or MIB entries in those methods are refering to text formatted OIDs.
There's many samples in the pyconfig.py file.
e.g : ('LLDP-MIB', 'lldpLocSysName', 0)
"""

#Potential deprecated method
def get_engine():
    mibBuilder = builder.MibBuilder()
    engine=SnmpEngine()
    builder_engine = engine.getMibBuilder()
    builder_engine.addMibSources(builder.DirMibSource('Scripts/python/snmp/mibs'))
    return engine


def set_oid(target, oids, credentials,data):
    """
Ejemplo n.º 5
0
    for i in range(count):
        try:
            error_indication, error_status, error_index, var_binds = next(handler)
            if not error_indication and not error_status:
                items = {}
                for var_bind in var_binds:
                    items[str(var_bind[0])] = cast(var_bind[1])
                result.append(items)
            else:
                raise RuntimeError('Got SNMP error: {0}'.format(error_indication))
        except StopIteration:
            break
    return result

def cast(value):
    try:
        return int(value)
    except (ValueError, TypeError):
        try:
            return float(value)
        except (ValueError, TypeError):
            try:
                return str(value)
            except (ValueError, TypeError):
                pass
    return value

hlapi.UsmUserData('SNMPUser1', authKey='AUTHPass1', privKey='PRIVPass1', authProtocol=hlapi.usmHMACSHAAuthProtocol, privProtocol=hlapi.usmAesCfb128Protocol)

print(get('192.168.229.10', [‘1.3.6.1.2.1.1.5.0’], hlapi.UsmUserData('SNMPUser1', authKey='AUTHPass1', privKey='PRIVPass1', authProtocol=hlapi.usmHMACSHAAuthProtocol, privProtocol=hlapi.usmAesCfb128Protocol)))