示例#1
0
class AbstractSession(object):
    def __init__(self, host, port=None):
        self.transportTarget = UdpTransportTarget((host, port))
        self.authData = None
        self.generator = CommandGenerator()

    def format_varBinds(self, varBinds):
        return [(str_to_oid(oid), value) for oid, value in varBinds]

    def format_varBindTable(self, varBindTable):
        result = []
        for varBinds in varBindTable:
            result.extend(self.format_varBinds(varBinds))
        return result

    def handle_error(self, errorIndication, errorStatus, errorIndex,
                     varBinds=None, varBindTable=None):
        if errorIndication:
            raise PySNMPError(errorIndication)
        elif errorStatus:
            variables = varBinds or varBindTable[-1]
            text = errorStatus.prettyPrint()
            position = errorIndex and variables[int(errorIndex) - 1] or '?'
            raise PySNMPError("%s at %s" % (text, position))

    def get(self, *args):
        try:
            errorIndication, errorStatus, \
                errorIndex, varBinds = self.generator.getCmd(
                    self.authData, self.transportTarget, *args)
        except pysnmp_error.PySnmpError as e:
            # handle origin PySNMPError from pysnmp module.
            errorIndication = e
            errorStatus, errorIndex, varBinds = None, None, []

        self.handle_error(errorIndication, errorStatus, errorIndex, varBinds)
        return self.format_varBinds(varBinds)

    def set(self, *args):
        errorIndication, errorStatus, \
            errorIndex, varBinds = self.generator.setCmd(
                self.authData, self.transportTarget, *args)
        self.handle_error(errorIndication, errorStatus, errorIndex, varBinds)
        return self.format_varBinds(varBinds)

    def getnext(self, *args):
        errorIndication, errorStatus, errorIndex, \
            varBindTable = self.generator.nextCmd(
                self.authData, self.transportTarget, *args)
        self.handle_error(
            errorIndication, errorStatus, errorIndex, None, varBindTable)
        return self.format_varBindTable(varBindTable)

    def getbulk(self, rows, *args):
        errorIndication, errorStatus, errorIndex, \
            varBindTable = self.generator.bulkCmd(
                self.authData, self.transportTarget, 0, rows, *args)
        self.handle_error(
            errorIndication, errorStatus, errorIndex, None, varBindTable)
        return self.format_varBindTable(varBindTable)
示例#2
0
class AbstractSession(object):
    def __init__(self, host, port=None):
        self.transportTarget = UdpTransportTarget((host, port))
        self.authData = None
        self.generator = CommandGenerator()

    def format_varBinds(self, varBinds):
        return [(str_to_oid(oid), value) for oid, value in varBinds]

    def format_varBindTable(self, varBindTable):
        result = []
        for varBinds in varBindTable:
            result.extend(self.format_varBinds(varBinds))
        return result

    def handle_error(self,
                     errorIndication,
                     errorStatus,
                     errorIndex,
                     varBinds=None,
                     varBindTable=None):
        if errorIndication:
            raise PySNMPError(errorIndication)
        elif errorStatus:
            vars = varBinds or varBindTable[-1]
            text = errorStatus.prettyPrint()
            position = errorIndex and vars[int(errorIndex) - 1] or '?'
            raise PySNMPError("%s at %s" % (text, position))

    def get(self, *args):
        errorIndication, errorStatus, \
                 errorIndex, varBinds = self.generator.getCmd(self.authData, self.transportTarget, *args)
        self.handle_error(errorIndication, errorStatus, errorIndex, varBinds)
        return self.format_varBinds(varBinds)

    def set(self, *args):
        errorIndication, errorStatus, \
                 errorIndex, varBinds = self.generator.setCmd(self.authData, self.transportTarget, *args)
        self.handle_error(errorIndication, errorStatus, errorIndex, varBinds)
        return self.format_varBinds(varBinds)

    def getnext(self, *args):
        errorIndication, errorStatus, errorIndex, \
                 varBindTable = self.generator.nextCmd(self.authData, self.transportTarget, *args)
        self.handle_error(errorIndication, errorStatus, errorIndex, None,
                          varBindTable)
        return self.format_varBindTable(varBindTable)

    def getbulk(self, rows, *args):
        errorIndication, errorStatus, errorIndex, \
                 varBindTable = self.generator.bulkCmd(self.authData, self.transportTarget, 0, rows, *args)
        self.handle_error(errorIndication, errorStatus, errorIndex, None,
                          varBindTable)
        return self.format_varBindTable(varBindTable)
示例#3
0
from argparse import ArgumentParser
from pysnmp.entity.rfc3413.oneliner.cmdgen import (
    CommandGenerator,
    CommunityData,
    UdpTransportTarget,
    UsmUserData,
    usmAesCfb128Protocol,
    usmDESPrivProtocol,
    usmHMACSHAAuthProtocol,
)
from pysnmp.proto.rfc1902 import Integer, Counter32, Counter64
import re
import sys

# Predefine some variables, it makes this program run a bit faster.
cmd_gen = CommandGenerator()

OIDS = {
    'if_index': '1.3.6.1.2.1.2.2.1.1',  # Index of all ports.
    'if_admin_status': '1.3.6.1.2.1.2.2.1.7',  # Status of port. Up="1"
    'if_oper_status': '1.3.6.1.2.1.2.2.1.8',  # Status of port. Up="1"
    'if_name': '1.3.6.1.2.1.31.1.1.1.1',  # name / display-string of port.
    'if_alias': '1.3.6.1.2.1.31.1.1.1.18',  # name / display-string of port.
    'switch_model': '.1.3.6.1.2.1.1.1.0',
    'port_name': '.1.3.6.1.2.1.31.1.1.1.1',
    'port_state': '1.3.6.1.2.1.2.2.1.8',
}

LAGG_OIDS = {
    'procurve': '.1.3.6.1.4.1.11.2.14.11.5.1.7.1.3.1.1.8',
    'powerconnect': '.1.2.840.10006.300.43.1.2.1.1.12',
示例#4
0
 def __init__(self, host, port=None):
     self.transportTarget = UdpTransportTarget((host, port))
     self.authData = None
     self.generator = CommandGenerator()
示例#5
0
 def __init__(self, host, port=None):
     self.transportTarget = UdpTransportTarget((host, port))
     self.authData = None
     self.generator = CommandGenerator()