Data Types¶
This section describes several classes that implement types defined in RFC 3416, Section 3.
SimpleSyntax¶
- class snmp.smi.Integer32(value: int)¶
The default
INTEGERtype for SNMP.The constructor raises a
ValueErrorif the value is not in the range of a 32-bit two’s complement number.- property value: int¶
This object’s value as a native
int.
- __eq__(self, other: object) bool¶
Compare two
INTEGERs for equality both in value and in ASN.1 type. AnInteger32and anUnsigned32with the same value are not equal because they differ in type.
- class snmp.smi.OctetString(data: bytes)¶
A representation of an ASN.1
OCTET STRING.The constructor raises a
ValueErrorif the data is longer than 65535 bytes.- property data: bytes¶
The object’s raw data as a native
bytes.
- __eq__(self, other: object) bool¶
Compare two
OCTET STRIGSfor equality both in value and in ASN.1 type. AnOctetStringand anOpaquecontaining the same data are not equal because they differ in type.
- class snmp.smi.OID(*subidentifiers)¶
A representation of an ASN.1
OBJECT IDENTIFIER.The subidentifiers argument list accepts between 0 and 128 integers. The first sub-identifier must be between
0and2, the second must be between0and39, and the rest must be between0to(2^32)-1. The encoding rules do not support OIDs with less than 2 sub-identifiers, so when sent over the wire, the objectsOID(),OID(0),OID(1), andOID(2)becomeOID(0, 0),OID(0, 0),OID(1, 0), andOID(2, 0), respectively.The methods in this class are all non-mutating. The descriptions sometimes use words like “append” as shorthand for “return a new instance containing the sub-identifiers of this object followed by one or more additional subidentifier(s).”
- classmethod parse(oid: str) OID¶
Convert a string like
"1.3.6.1.2.1.1.1.0"or".1.3.6.1.2.1.1.1.0"into anOID. Raise aValueErrorif oid does not represent a validOID.
- __len__() int¶
Count the number of sub-identifiers.
- __getitem__(n: int | slice) int | tuple[int, ...]¶
Retrieve the sub-identifier(s) at index (or slice) n.
- __iter__() Iterator[int]¶
Iterate through the sub-identifiers.
- extend(*subidentifiers: int) OID¶
Append the given subidentifiers. Raise a
ValueErrorif the result would be longer than 128 sub-identifiers.
- withIndex(*index: Integer | OctetString | OID, implied: bool = False) OID¶
Construct an SNMP variable name from an object type name and an index. This operation is the reverse of
getIndex()anddecodeIndex().For example, the following code requests the description of interface 3:
ifDescr = OID.parse("1.3.6.1.2.1.2.2.1.2") vb, = manager.get(ifDescr.withIndex(Integer(3))) print(f"Description: {vb.value.data}")
In rare cases, the last variable in the
INDEXclause is labelled asIMPLIED. For example, here’s a definition from theSNMP-NOTIFICATION-MIB:snmpNotifyFilterEntry OBJECT-TYPE SYNTAX SnmpNotifyFilterEntry MAX-ACCESS not-accessible STATUS current DESCRIPTION "An element of a filter profile. Entries in the snmpNotifyFilterTable are created and deleted using the snmpNotifyFilterRowStatus object." INDEX { snmpNotifyFilterProfileName, IMPLIED snmpNotifyFilterSubtree } ::= { snmpNotifyFilterTable 1 }When encoding such an index, you must set the implied argument to
True.This method raises a
ValueErrorif the result would be longer than 128 sub-identifiers.Additional Reading¶
RFC 1157, Section 3.2.6.3 explains the relationship between the “names” of object types, and the “names” of the instances of an object type. The word “names” means OIDs.
RFC 2578, Section 7.7 specifies how an object is encoded as an
INDEX, including the meaning of theIMPLIEDkeyword.
- getIndex(prefix: OID, cls=Integer, implied=False)¶
Decode the index portion of an SNMP variable name, so long as the index is a single value. For a multi-part index, use
decodeIndex().For example, this snippet requests the first interface description in
ifTable, and uses the returnedOIDto determine itsifIndex.ifDescr = OID.parse("1.3.6.1.2.1.2.2.1.2") vb, = manager.getNext(ifDescr) index = vb.name.getIndex(ifDescr).value description = vb.value.data print(f"Description for interface {index}: {description}")
You can also use the cls parameter to decode a non-
Integerindex. Here is a contrived example of this:prefix = OID.parse("1.2.3.4") oid = prefix.extend(5, 1, 2, 3, 4, 5) index = oid.getIndex(prefix, OID) print(index) # prints "1.2.3.4.5"
In rare cases, the variable in the
INDEXclause is labelled asIMPLIED. For example, here’s a definition from theSNMP-NOTIFICATION-MIB:snmpNotifyEntry OBJECT-TYPE SYNTAX SnmpNotifyEntry MAX-ACCESS not-accessible STATUS current DESCRIPTION "An entry in this table selects a set of management targets which should receive notifications, as well as the type of notification which should be sent to each selected management target. Entries in the snmpNotifyTable are created and deleted using the snmpNotifyRowStatus object." INDEX { IMPLIED snmpNotifyName } ::= { snmpNotifyTable 1 }When decoding such an index, you must set the implied argument to
True.If the
OIDdoes not begin with prefix, this method will raise aBadPrefixexception. If there is a problem decoding the index, it will raise anIndexDecodeError.
- decodeIndex(prefix: OID, *types: type, implied=False) tuple[...]¶
Decode the index portion of an SNMP variable name.
Note
This method always returns the index as a tuple, even if the length is 1. It is therefore recommended to use
getIndex()when the index consists of a single value.Here’s a real object type definition from the
IP-MIB:ipAddressPrefixEntry OBJECT-TYPE SYNTAX IpAddressPrefixEntry MAX-ACCESS not-accessible STATUS current DESCRIPTION "An entry in the ipAddressPrefixTable." INDEX { ipAddressPrefixIfIndex, ipAddressPrefixType, ipAddressPrefixPrefix, ipAddressPrefixLength } ::= { ipAddressPrefixTable 1 }Notice that there are four variables in the
INDEX. You can determine the data type of each variable by tracing theirSYNTAXclauses until you get to a primitive type. You can then decode the index by passing the corresponding classes todecodeIndex()via the types argument list. Here is a code sample that does just that:ipAddressPrefixOrigin = OID.parse("1.3.6.1.2.1.4.32.1.5") vb, = manager.getNext(ipAddressPrefixOrigin) index = vb.name.decodeIndex( ipAddressPrefixOrigin, Integer, Integer, OctetString, Integer, ) print(f"ifIndex: {index[0].value}") print(f"type: {index[1].value}") print(f"prefix: {index[2].data}") print(f"length: {index[3].value}") print(f"origin: {vb.value.value}")
In rare cases, the last variable in the
INDEXclause is labelled asIMPLIED. For example, here’s a definition from theSNMP-NOTIFICATION-MIB:snmpNotifyFilterEntry OBJECT-TYPE SYNTAX SnmpNotifyFilterEntry MAX-ACCESS not-accessible STATUS current DESCRIPTION "An element of a filter profile. Entries in the snmpNotifyFilterTable are created and deleted using the snmpNotifyFilterRowStatus object." INDEX { snmpNotifyFilterProfileName, IMPLIED snmpNotifyFilterSubtree } ::= { snmpNotifyFilterTable 1 }When decoding such an index, you must set the implied argument to
True.If the
OIDdoes not begin with prefix, this method will raise aBadPrefixexception. If there is a problem decoding the index, it will raise anIndexDecodeError.
- exception BadPrefix¶
- exception IndexDecodeError¶
ApplicationSyntax¶
- class snmp.smi.IpAddress(addr: str)¶
An IPv4 address. The constructor raises a
ValueErrorif the addr is not a valid IPv4 address string.This is an
OCTET STRINGtype, so it implements the same interface asOctetString.- property addr: str¶
The address in human-readable
"X.X.X.X"format.
- property data: bytes¶
A byte string encoding the address in network format.
- class snmp.smi.Counter32(value: int)¶
This class is exactly like
Integer32, but allows only 32-bit unsigned values.- property value: int¶
This object’s value as a native
int.
- __eq__(self, other: object) bool¶
Compare two
INTEGERs for equality both in value and in ASN.1 type.
- class snmp.smi.Unsigned¶
An alias for
Unsigned32.
- class snmp.smi.Unsigned32(value: int)¶
This class is exactly like
Integer32, but allows only 32-bit unsigned values.- property value: int¶
This object’s value as a native
int.
- __eq__(self, other: object) bool¶
Compare two
INTEGERs for equality both in value and in ASN.1 type.
- class snmp.smi.Gauge32(value: int)¶
Indistinguishable from
Unsigned32in all but name.
- class snmp.smi.TimeTicks(value: int)¶
This class is exactly like
Integer32, but allows only 32-bit unsigned values.- property value: int¶
This object’s value as a native
int.
- __eq__(self, other: object) bool¶
Compare two
INTEGERs for equality both in value and in ASN.1 type.
- class snmp.smi.Opaque(data: bytes)¶
This class is exactly like
OctetString.- property data: bytes¶
The object’s raw data as a native
bytes.
- __eq__(self, other: object) bool¶
Compare two
OCTET STRIGSfor equality both in value and in ASN.1 type.
Variable Bindings¶
- class snmp.smi.Null¶
A placeholder value for requests.
- class snmp.smi.NoSuchObject¶
A special value in a response that means that the requested OID does not refer to a known object type.
- __eq__(self, other: object) bool¶
Check if other is an instance of
NoSuchObject.
- class snmp.smi.NoSuchInstance¶
A special value in a response meaning that the OID refers to a known object type, but there is no instance with the requested OID index.
- __eq__(self, other: object) bool¶
Check if other is an instance of
NoSuchInstance.
- class snmp.smi.EndOfMibView¶
A special value in a response to a GetNext or GetBulk request meaning that there are no variables following the requested OID.
- __eq__(self, other: object) bool¶
Check if other is an instance of
EndOfMibView.
- class snmp.smi.VarBind(oid: OID | str, value: Optional[Integer | OctetString | OID] = None)¶
-
- property value: Integer | OctetString | Null | OID¶
The variable value, which will be one of the types described on this page.