Esempio n. 1
0
def NativeVal2Snmp(nativeVal, snmpValueType):
    """
    :param nativeVal: native for python values, i.e. int/float/str etc.
    :return: The SNMP suitable value
    """
    if snmpValueType is None:
        # Try to autodetect desired SNMP value type
        if isinstance(nativeVal, int):
            result = v2c.Integer(nativeVal)
        elif isinstance(nativeVal, float):
            result = v2c.Integer(nativeVal)
        elif isinstance(nativeVal, str):
            if IsValidIpv4Address(nativeVal):
                result = v2c.IpAddress(nativeVal)
            else:
                result = v2c.OctetString(nativeVal)
        else:
            raise TypeError(
                "Unable to autodetect SNMP value type. Please supply "
                "the snmpValueType argument."
                ", ".join(list(TYPES.keys())))
    else:
        if snmpValueType not in TYPES:
            raise ValueError(
                "'{}' is not one of the supported types: {}".format(
                    snmpValueType, ", ".join(list(TYPES.keys()))))

        result = TYPES[snmpValueType](nativeVal)
    return result
Esempio n. 2
0
# PDU v1/v2c two-way proxy
from pysnmp.proto import rfc1905, rfc3411, error
from pysnmp.proto.api import v1, v2c
from pysnmp import debug

# 2.1.1

__v1ToV2ValueMap = {
    v1.Integer.tagSet: v2c.Integer32(),
    v1.OctetString.tagSet: v2c.OctetString(),
    v1.Null.tagSet: v2c.Null(),
    v1.ObjectIdentifier.tagSet: v2c.ObjectIdentifier(),
    v1.IpAddress.tagSet: v2c.IpAddress(),
    v1.Counter.tagSet: v2c.Counter32(),
    v1.Gauge.tagSet: v2c.Gauge32(),
    v1.TimeTicks.tagSet: v2c.TimeTicks(),
    v1.Opaque.tagSet: v2c.Opaque()
}

__v2ToV1ValueMap = {  # XXX do not re-create same-type items?
    v2c.Integer32.tagSet: v1.Integer(),
    v2c.OctetString.tagSet: v1.OctetString(),
    v2c.Null.tagSet: v1.Null(),
    v2c.ObjectIdentifier.tagSet: v1.ObjectIdentifier(),
    v2c.IpAddress.tagSet: v1.IpAddress(),
    v2c.Counter32.tagSet: v1.Counter(),
    v2c.Gauge32.tagSet: v1.Gauge(),
    v2c.TimeTicks.tagSet: v1.TimeTicks(),
    v2c.Opaque.tagSet: v1.Opaque()
}
Esempio n. 3
0
# Build and submit notification message to dispatcher
ntfOrg.sendVarBinds(
    snmpEngine,
    # Notification targets
    'my-notification',  # notification targets
    None,
    '',  # contextEngineId, contextName
    # var-binds
    [
        # Uptime value with 12345
        (v2c.ObjectIdentifier('1.3.6.1.2.1.1.3.0'), v2c.TimeTicks(12345)),
        # trap OID: Generic Trap #6 (enterpriseSpecific)
        #           and Specific Trap 432
        (v2c.ObjectIdentifier('1.3.6.1.6.3.1.1.4.1.0'),
         v2c.ObjectIdentifier('1.3.6.1.4.1.20408.4.1.1.2.0.432')),
        # Agent Address with '127.0.0.1'
        (v2c.ObjectIdentifier('1.3.6.1.6.3.18.1.3.0'),
         v2c.IpAddress('127.0.0.1')),
        # Enterprise OID with 1.3.6.1.4.1.20408.4.1.1.2
        (v2c.ObjectIdentifier('1.3.6.1.6.3.1.1.4.3.0'),
         v2c.ObjectIdentifier('1.3.6.1.4.1.20408.4.1.1.2')),
        # managed object '1.3.6.1.2.1.1.1.0' = 'my system'
        (v2c.ObjectIdentifier('1.3.6.1.2.1.1.1.0'),
         v2c.OctetString('my system'))
    ])

print('Notification is scheduled to be sent')

# Run I/O dispatcher which would send pending message and stop
snmpEngine.transportDispatcher.runDispatcher()