pysnmp 快速入门 简单测试代码

转载 2018年04月17日 12:50:14

文档转载自:
http://pysnmp.sourceforge.net/quick-start.html#fetch-snmp-variable

Quick start

Once you downloaded and installed PySNMP library on your Linux/Windows/OS X system, you should be able to solve the very basic SNMP task right from your Python prompt - fetch some data from a remote SNMP Agent (you’d need at least version 4.3.0 to run code from this page).

Fetch SNMP variable

So just cut&paste the following code right into your Python prompt. The code will performs SNMP GET operation for a sysDescr.0 object at a publically available SNMP Command Responder at demo.snmplabs.com:
http://snmplabs.com/snmpsim/index.html
http://snmpsim.sourceforge.net/public-snmp-simulator.html

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from pysnmp.hlapi import *

errorIndication, errorStatus, errorIndex, varBinds = next(
    getCmd(SnmpEngine(),
           CommunityData('public', mpModel=0),
           UdpTransportTarget(('demo.snmplabs.com', 161)),
           ContextData(),
           ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)))
)

if errorIndication:
    print(errorIndication)
elif errorStatus:
    print('%s at %s' % (errorStatus.prettyPrint(),
                        errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
else:
    for varBind in varBinds:
        print(' = '.join([x.prettyPrint() for x in varBind]))

If everything works as it should you will get:

...
SNMPv2-MIB::sysDescr."0" = SunOS zeus.snmplabs.com 4.1.3_U1 1 sun4m
>>>

或者是

SNMPv2-MIB::sysDescr.0 = Linux zeus 4.8.6.5-smp #2 SMP Sun Nov 13 14:58:11 CDT 2016 i686

on your console.

Send SNMP TRAP

To send a trivial TRAP message to our hosted Notification Receiver at demo.snmplabs.com , just cut&paste the following code into your interactive Python session:

from pysnmp.hlapi import *

errorIndication, errorStatus, errorIndex, varBinds = next(
    sendNotification(
        SnmpEngine(),
        CommunityData('public', mpModel=0),
        UdpTransportTarget(('demo.snmplabs.com', 162)),
        ContextData(),
        'trap',
        NotificationType(
            ObjectIdentity('1.3.6.1.6.3.1.1.5.2')
        ).addVarBinds(
            ('1.3.6.1.6.3.1.1.4.3.0', '1.3.6.1.4.1.20408.4.1.1.2'),
            ('1.3.6.1.2.1.1.1.0', OctetString('my system'))
        )
    )
)
if errorIndication:
    print(errorIndication)

Many ASN.1 MIB files could be downloaded from mibs.snmplabs.com or PySNMP could be configured to download them automatically.

For more sophisticated examples and use cases please refer to examples and library reference pages.

Android项目实战:简单天气

学习新的知识点,时下Android比较流行的MPAndroidChart + Retrofit2.0 + Gson!
  • 2016年11月25日 09:21

PySNMP学习笔记(二)

原文地址:PySNMP学习笔记(二)作者:MichaelLing83关于getCmd的返回值。 The varBinds is a tuple of Managed Objects. Managed...
  • esonbest1234
  • esonbest1234
  • 2016-02-24 11:45:19
  • 356

pysnmp学习(一)

安装pysnmp: 编译安装的话,pysnmp需要用到asn.1,安装pysnmp的话需要先安装asn.1,不过我没有安装成功。 也可以使用easy_install pysnmp安...
  • shangzhihaohao
  • shangzhihaohao
  • 2011-11-19 23:15:27
  • 7992

PySNMP官方文档翻译---常用操作

常用操作在这个教程里,我们将会逐渐学习创建并运行一些不同的SNMP命令请求和通告。我们将使用PySNMP同步的使用最简单的高级API创建SNMP Engine在PySNMP里,SNMP Engine是...
  • u014793472
  • u014793472
  • 2016-09-23 17:34:59
  • 2437

PySNMP--GET command

GET commandpysnmp.hlapi.getCmd(snmpEngine, authData, transportTarget, contextData, *varBinds, **opti...
  • u014793472
  • u014793472
  • 2017-02-23 10:25:53
  • 361

ubuntu14.04 使用pysnmp SNMP trap 接受

下载 安装pysnmp-4.2.5.tar.gz (md5) https://pypi.python.org/pypi/pysnmp/ from pysnmp.carrier.asynso...
  • cauckfgf
  • cauckfgf
  • 2015-07-02 10:59:38
  • 1262

PySNMP扩展snmp-trap

pysnmp是用python语言开发的snmp开源项目,感觉没有net-snmp强大。但如果你只开发snmp 客户端的 trap,那么用pysnmp还是很简单的。 1、安装下载pysnmp和pyasn...
  • sealyao
  • sealyao
  • 2009-09-12 10:18:00
  • 7193

python利用pysnmp获取交换机级联接口

交换机里有一个arp表,利用arp表可以找到与其相连的交换机的mac地址,然后再从mac端口对应表里找出端口 def getTrunk(ip,port,agent,communication...
  • shangzhihaohao
  • shangzhihaohao
  • 2011-11-30 22:05:18
  • 5312

pysnmp的安装

pysnmp的安装   pysnmp依赖两个第三方的python库: (1) ASN.1 : Structure of Management Information:管理信息结构 用于定义...
  • yshh126
  • yshh126
  • 2015-07-16 17:30:19
  • 857

PySNMP学习笔记(一)

转自:http://blog.sina.com.cn/s/blog_54ce569c01009ccb.html SNMP standard introduces a set of ASN.1 lan...
  • wujingwen1111
  • wujingwen1111
  • 2014-02-12 10:19:41
  • 947
收藏助手
不良信息举报
您举报文章:pysnmp 快速入门 简单测试代码
举报原因:
原因补充:

(最多只允许输入30个字)