PySNMP学习笔记(一)

来源:互联网 发布:linux搭建jenkins环境 编辑:程序博客网 时间:2024/05/17 23:40

转自:http://blog.sina.com.cn/s/blog_54ce569c01009ccb.html

SNMP standard introduces a set of ASN.1 language constructs (such as ASN.1 subtypes and MACROs) which is called Structure of Management Information (SMI). Collections of related Managed Objects described in terms of SMI comprise Management Information Base (MIB) modules.
SNMP标准引入一组ASN.1语言元素,称之为SMI(Structure of Management Information)。由SMI描述的相互关联的被管对象(Managed Objects)组成MIB(Management Information Base)模块。

Commonly used Managed Objects form core MIBs that become part of SNMP standard. The rest of MIBs are normally created by vendors who build SNMP Agents into their products.
核心MIB中经常用到的Managed Objects成为SNMP标准的一部分。剩下的MIB一般由设备生产商在其设备中创建。(也就是说这些MIB是生产商和设备相关的)

PySNMP是一个纯粹用Python实现的SNMP。

使用PySNMP的最抽象的API为One-line Applications。其中有两类API:同步的和非同步的,都在模块
pysnmp.entity.rfc3413.oneliner.cmdgen 中实现。

所以在使用的时候为了方便,可以先

from pysnmp.entity.rfc3413.oneliner import cmdgen
然后用
cg = cmdgen.CommandGenerator()
来产生一个CommandGenerator对象,以后调用cg的getCmd等方法来获取或者设置网络设备信息。



同步One-line Applications:

所有的Command Generator Applications在同一个类中实现:

class CommandGenerator([snmpEngine])

产生一个SNMP Command Generator对象。

CommandGenerator对象的方法实现特定的请求类型。

方法有:

getCmdauthDatatransportTarget*varNames )

执行SNMP GET请求,并返回一个响应或者指示错误。

authData是一个SNMP安全参数对象(Security Parameters object),transportTarget是一个SNMP传输配置对象(Transport Configuration object),*varNames是OID。

方法getCmd返回由errorIndicationerrorStatuserrorIndexvarBinds组成的一个tuple。

非空的errorIndication字符串指示SNMP引擎级别的错误(engine-level error)。

变量对errorStatuserrorIndex指示SNMP的PDU级别错误(PDU-level error)。如果errorStatus等于true,表明SNMP的PDU错误由varBinds索引为errorIndex-1的被管对象引起。

varBinds是由被管对象(Managed Objects)组成的tuple。响应中的被管对象位置与传递参数时的OID相同。每一个被管对象都是由Object NameObject Value组成的tuple。

安全配置:

Calls to one-line Applications API require Security Parameters and Transport configuration objects as input parameters. These classes serve as convenience shortcuts to SNMP engine configuration facilities and for keeping persistent authentication/transport configuration between SNMP engine calls.

one-line Application API的参数需要Security Parameters和Transport configuration对象作为参数。这两个类是使用SNMP引擎配置工具的快捷方式,并且用来持久的在多次SNMP引擎调用之间保存认证和传输配置。

Security Parameters object is Security Model specific. UsmUserData class serves SNMPv3 User-Based Security Model configuration, while CommunityData class is used for Community-Based Security Model of SNMPv1/SNMPv2c.

Security Parameters对象是安全模型相关的。类UsmUserData类用于SNMPv3的User-Based Security Model的配置,而CommunityData类用在SNMPv1/SNMPv2c中的Community-Based Security模型。


class CommunityDatasecurityNamecommunityNamempModel=1 )

Create an object holding Community-Based Security Model specific configuration parameters.

必须的参数securityName是基于社区安全模型的用户名(字符串),在绝大多数情况下这可以是一个任意的字符串。

必须的参数communityName是SNMPv1/SNMPv2c的社区名字(字符串)。(这个应该就是我们使用的密码了,有public、private,或者是管理员另外配置的一个相当于密码的字符串。如果不对的话,Agent是不产生响应的。)

额外的参数mpModel指示使用SNMPv2c(mpModel=1,默认)还是使用SNMPv1(mpModel=0)协议。


传输配置:

Transport configuration object is Transport domain specific. UdpTransportTarget class represents an Agent accessible through UDP domain transport.

传输配置对象是传输域相关的。UdpTransportTarget类代表一个可以通过UDP传输到达的Agent。

class UdpTransportTargettransportAddr )

Create an object representing a single Agent accessible through UDP socket.

必须的参数transportAddr指定目标Agent的地址,格式为由FQDN、端口组成的二元tuple,其中FQDN是字符串,而端口是整数。(这里的FQDN就是ip地址,或者是'localhost'这样可以解析为ip地址的主机名)。


产生PySNMP中使用的OID:

from pysnmp.proto.rfc1902 import ObjectName
oid = ObjectName( '1.2.3.4.5.6' )
以后在使用OID的地方就可以用变量oid了。

0 0
原创粉丝点击