Skip to main content

GoSNMP Python

Project description

gosnmp-python

The purpose of this module is to provide a Python interface to the Golang gosnmp module.

It was made very easy with the help of the Golang gopy module.

Versions

This version (0.91) is the last version to support Python 2; all versions after this have been subject to a refactor and support Python 3 only.

Limitations

  • Python command needs to be prefixed with GODEBUG=cgocheck=0 (or have that in the environment)
  • I've not implemented walk (as I didn't need it for my use-case, I just use get_next with Python)
  • Seems to have some odd memory problems with PyPy (via CFFI); lots of locks and stuff to try and alleviate that

Prerequisites

  • Go 1.13
  • Python 2.7+
  • pip
  • virtualenvwrapper
  • pkgconfig/pkg-config

Installation (from PyPI)

  • python -m pip install gosnmp-python

Installation (for prod)

  • python setup.py install

Making a python wheel install file (for distribution)

  • python setup.py bdist_wheel

Setup (for dev)

Ensure both go and pkg-config are installed.

  • mkvirtualenvwrapper -p (/path/to/pypy) gosnmp-python
  • pip install -r requirements-dev.txt
  • ./build.sh
  • GODEBUG=cgocheck=0 py.test -v

What's worth knowing if I want to further the development?

  • gopy doesn't like Go interfaces; so make sure you don't have any public (exported) interfaces
    • this includes a struct with a public property that may eventually lead to an interface

Example Go RPCSession usage (simple session ID, calls return JSON)

There's no real reason why you'd want to do this (just use gosnmp on it's own)- it's more for reference:

package main

import (
	"gosnmp_python"
	"fmt"
)

func main() {

	sessionID := gosnmp_python.NewRPCSessionV2c(
		"1.2.3.4",
		161,
		"public",
		5,
		1,
	)

	err := gosnmp_python.RPCConnect(sessionID)
	if err != nil {
		panic(err)
	}

	jsonResult, err := gosnmp_python.RPCGet(sessionID, ".1.3.6.1.2.1.1.5.0")
	if err != nil {
		panic(err)
	}

	fmt.Println(jsonResult)

	err = gosnmp_python.RPCClose(sessionID)
	if err != nil {
		panic(err)
	}

}

Example Python usage (uses RPCSession underneath because of memory leaks between Go and Python with structs)

To create an SNMPv2 session in Python do the following:

from gosnmp_python import create_snmpv2c_session

session = create_snmpv2c_session(
    hostname='1.2.3.4',
    community='public',
)

session.connect()

print session.get('.1.3.6.1.2.1.1.5.0')

session.close()

Which returns an object like this:

SNMPVariable(
    oid='.1.3.6.1.2.1.1.5', 
    oid_index=0, 
    snmp_type=u'bytearray', 
    value='hostname.domain.com.au'
)

Some of this may feel a bit like easysnmp; that's intentional, I was originally using that but I think its got some underlying thread-safety issues on the C side (particularly to do with SNMPv3).

No offence to the guys that contribute to that project- it's served me very well.

To use the test build container...

./test.sh

To shell into the test container to have a look around...

./test.sh bash

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gosnmp-python-0.91.tar.gz (2.5 MB view details)

Uploaded Source

File details

Details for the file gosnmp-python-0.91.tar.gz.

File metadata

  • Download URL: gosnmp-python-0.91.tar.gz
  • Upload date:
  • Size: 2.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.7.1 requests/2.26.0 setuptools/44.1.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/2.7.16

File hashes

Hashes for gosnmp-python-0.91.tar.gz
Algorithm Hash digest
SHA256 f615691d83e7ba58114bbb98d71db0a44eabef1d2b2a08db3a0728ad733b308d
MD5 2bc5c06b5bf37c4b78ec7ee029c6c3b6
BLAKE2b-256 3aac46207e752f0176a0ee022f6534b5080b50d5760754fcfcdf4a314b7a8014

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page