PySNMP學習筆記(一)

轉自: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了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章