Skip to main content

Build Status License Language

Scrippy, my scrangourou friend

scrippy_snmp

Basic SNMP capabilities for the Scrippy framework.

Prerequisites

Python

Python >= 3.10 (tested with Python 3.11).

Python modules

List of required modules

The modules listed below will be automatically installed.

  • pysnmp

Installation

Manual

git clone https://codeberg.org/scrippy/scrippy-snmp.git
cd scrippy-snmp
python -m pip install -r requirements.txt
make install

With pip

pip install scrippy-snmp

Usage

This module allows basic SNMP operations such as get, walk and set.

it supports SNMP version 1 and version 2. Version 3 is still in depveloppment.

Limitation: asynchronous contexts

The Snmp methods (get, walk and set) are synchronous: each call blocks until the remote host answers (or the timeout expires) and returns the result directly.

Internally, they rely on the asyncio API of pysnmp and run each request with asyncio.run(). As a consequence, they cannot be called from a thread where an asyncio event loop is already running, for example:

  • from within an async def function,
  • from a Jupyter notebook,
  • from an asynchronous framework (FastAPI, aiohttp, ...).

In such a context, the call raises:

RuntimeError: asyncio.run() cannot be called from a running event loop

If you need to use this module from asynchronous code, run the call in a separate thread:

import asyncio
from scrippy_snmp.snmp import Snmp

async def main():
  snmp = Snmp(host=HOST, port=PORT, community=RO_COMMUNITY, version=VERSION)
  location = await asyncio.to_thread(snmp.get, LOCATION_OID)

Regular (synchronous) scripts are not affected by this limitation.

SNMP operations

Setup

As in shell SNMP implementation it is first needed to set the connection up, typically by providing the remote hostname or IP address, the UDP port on which join the host and the community string which is a sort of secret identifier that is allowed to perform some operations on the remote host.

from scrippy_snmp.snmp import Snmp

HOST = "127.0.0.1"
PORT = 161
RO_COMMUNITY = "read-only-community"
RW_COMMUNITY = "read-write-community"
VERSION = "2c"

snmp = Snmp(host=HOST, port=PORT, community=RO_COMMUNITY, version=VERSION)
Get

The get operation allows to retrieve a specific OID value:

LOCATION_OID = ".1.3.6.1.2.1.1.6.0"

snmp = Snmp(host=HOST, port=PORT, community=RO_COMMUNITY, version=VERSION)
location = snmp.get(LOCATION_OID)

print(location)
("SNMPv2-MIB::sysLocation.0", "Room 42")

The return value is a 2 elements Tuple containing:

  • The OID in text format
  • The OID value
Walk

The walk operation allow to retrieve a collection of OID value directly under the specified OID

SYSTEM_OID = ".1.3.6.1.2.1.1"

snmp = Snmp(host=HOST, port=PORT, community=RO_COMMUNITY, version=VERSION)
result = snmp.walk(SYSTEM_OID)

for value in result:
  print(value)

('SNMPv2-MIB::sysDescr.0', 'arcadia.local.mcos.nc 1148685499 FreeBSD 13.2-RELEASE')
('SNMPv2-MIB::sysObjectID.0', 'SNMPv2-SMI::enterprises.12325.1.1.2.1.1')
('SNMPv2-MIB::sysUpTime.0', '320223')
('SNMPv2-MIB::sysContact.0', 'sysmeister@example.com')
('SNMPv2-MIB::sysName.0', 'scrippy.example.com')
('SNMPv2-MIB::sysLocation.0', 'Room 42')
...

The return value is a list of 2 elements Tuples, each containing:

  • The OID in text format
  • The OID value
Set

The set operation allows to set the value of a specific writtable OID.

LOCATION_OID = ".1.3.6.1.2.1.1.6.0"

snmp = Snmp(host=HOST, port=PORT, community=RW_COMMUNITY, version=VERSION)
snmp.set(oid=LOCATION_OID, value="Room 200", datatype="str")
location = snmp.get(LOCATION_OID)

print(location)
("SNMPv2-MIB::sysLocation.0", "Room 200")

Note:

  • The Snmp object must have been set up with a community with write permission on the remote host
  • The datatype must be provided to the Snmp.set() method (see below for available datatypes)
  • The Snmp.set() method does not return any value.
Available data types
Datatype SNMP equivalent Info
null Null Null value
bits Bits The BITS construct represents an enumeration of named bits. This collection is assigned non-negative, contiguous values, starting at zero.
int Integer32 The Integer32 type represents integer-valued information between -2^31 and 2^31-1 inclusive
str OctetString The OCTET STRING type represents arbitrary binary or textual data. Size limit is variable. 255 octets allow max interoperability
ipaddr IpAddress The IpAddress type represents a 32-bit internet address. It is represented as an OCTET STRING of length 4, in network byte-order
counter Counter32 The Counter32 type represents a non-negative integer which monotonically increases until it reaches a maximum value of 2^32
counter64 Counter64 The Counter64 type represents a non-negative integer which monotonically increases until it reaches a maximum value of 2^64-1, when it wraps around and starts increasing again from zero
gauge Gauge32 The Gauge32 type represents a non-negative integer, which may increase or decrease, but shall never exceed a maximum value, nor fall below a minimum value. The maximum value can not be greater than 2^32-1
unisgned Unsigned32 The Unsigned32 type represents integer-valued information between 0 and 2^32-1 inclusive (0 to 4294967295 decimal)
ticks TimeTicks The TimeTicks type represents a non-negative integer which represents the time, modulo 2^32 (4294967296 decimal), in hundredths of a second between two epochs.

Metadata

Release files for scrippy-snmp 2.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for scrippy-snmp 2.0.1
File Size Uploaded
scrippy_snmp-2.0.1.tar.gz 7.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for scrippy-snmp 2.0.1
File Interpreter ABI Platform
scrippy_snmp-2.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 14.7 kB

Release files / scrippy_snmp-2.0.1.tar.gz

Download URL scrippy_snmp-2.0.1.tar.gz
Size 7.8 kB
Tags Source
SHA-256 checksum
How to use checksums
a36f957a896531e48e663f934289e90b7552644a9369422ebad919b94b67ea7f
BLAKE2b-256 checksum
How to use checksums
d26225fbe54a23cd46e8eb743138ee9e3091003e1e5f3c7239a6267b0c95c142
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.17

Release files / scrippy_snmp-2.0.1-py3-none-any.whl

Download URL scrippy_snmp-2.0.1-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b21a580a4ae3d187ba2b854e15c91a2a6093b561feb8d11d81d5a873777bcba2
BLAKE2b-256 checksum
How to use checksums
ffe05b2b67836f78d61c10e11da55cbbe27be1de9c858e3ce79e2c9929d90ce0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.17

Release history Release notifications | RSS feed

This release

2.0.1 This release

2 release files

2.0.0

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page