Think before you speak, read before you think.

通过python netsnmp读取cisco交换机信息

首先使用snmpwalk跑一遍看一下有没有问题

snmpwalk -v 2c -c public 10.103.33.1

这里测试用交换机是 WS-C2960G-24TC-L,以下脚本用于读取管理ip,序列号,型号,主机名。思科的交换机snmp oid信息都可通过如下网址查询http://tools.cisco.com/Support/SNMP/do/BrowseOID.do

首先安装python的snmp依赖包

yum install net-snmp-python

获取信息的脚本

#!/usr/bin/env python
# by yangyang89
# using snmp get switch serial, model, manage ip ..
import netsnmp
import sys
import urllib
import urllib2

# reference python for linux and unix administration page 209
class Snmp(object):
    """A basic SNMP session"""
    def __init__(self,oid="sysDescr", Version=2):
        self.oid = oid
        self.version = Version
        self.destHost = sys.argv[1]
        self.community = sys.argv[2]

    def query(self):
        """Creates SNMP query session"""
        try:
            result = netsnmp.snmpwalk(self.oid, Version = self.version, DestHost = self.destHost, Community = self.community)
        except Exception, err:
            print err
            result = None
        return result

print sys.argv[1] + sys.argv[2]
if sys.argv[1] and sys.argv[2]:
    s = Snmp()
    #print s.query()
    #s.oid = "2.47.1.1.1.1.11.1001"
    #http://tools.cisco.com/Support/SNMP/do/BrowseOID.do
    s.oid = ".1.3.6.1.2.1.4.20.1.1" # manage ip ipAdEntAddr
    ip = s.query()
    telnet = ip[0]
    print "ip: " + telnet
    
    s.oid = ".1.3.6.1.4.1.9.3.6.3" # serial numbers chassisId
    serial = s.query()
    serial = serial[0]
    print "serial: " + serial
    
    s.oid = ".1.3.6.1.2.1.47.1.1.1.1" # product_model entPhysicalEntry
    product_model = s.query()
    product_model = product_model[1].split(' ')[0]
    print "product_model: " + product_model
    #print s.query()
    
    s.oid = ".1.3.6.1.4.1.9.2.1.3" # hostname hostName
    hostname = s.query()
    hostname = hostname[0]
    print "hostname: " + hostname


Comments

8 responses to “通过python netsnmp读取cisco交换机信息”

  1. 我已经安装了netsnmp,yum install net-snmp-python
    Loaded plugins: fastestmirror, priorities
    Loading mirror speeds from cached hostfile
    * addons: centos.01link.hk
    * base: centos.01link.hk
    * extras: centos.01link.hk
    * rpmforge: mirrors.digipower.vn
    * updates: centos.01link.hk
    Excluding Packages from CentOS-5 – Base
    Finished
    Excluding Packages from CentOS-5 – Updates
    Finished
    Setting up Install Process
    No package net-snmp-python available.

    但是在python里面还是无法import netsnmp
    >>> import netsnmp
    Traceback (most recent call last):
    File “”, line 1, in ?
    ImportError: No module named netsnmp
    >>>
    求指点下

  2. No package net-snmp-python available.

    没有安装成功

  3. 想获取 路由表怎么弄

  4. 用 python-pexpect 模拟执行命令,打印后解析。

  5. 请问windows 如何安装 net-snmp-python??
    pip install net-snmp-python 找不到。。

  6. [root@localhost ~]# yum info net-snmp-python
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
     * base: mirrors.zju.edu.cn
     * extras: mirrors.163.com
     * updates: mirrors.163.com
    Installed Packages
    Name        : net-snmp-python
    Arch        : x86_64
    Epoch       : 1
    Version     : 5.7.2
    Release     : 24.el7_3.2
    Size        : 96 k
    Repo        : installed
    From repo   : updates
    Summary     : The Python 'netsnmp' module for the Net-SNMP
    URL         : http://net-snmp.sourceforge.net/
    License     : BSD
    Description : The 'netsnmp' module provides a full featured, tri-lingual SNMP (SNMPv3,
                : SNMPv2c, SNMPv1) client API. The 'netsnmp' module internals rely on the
                : Net-SNMP toolkit library.
    

    用 163 的 epel 源就能装

  7. centos 用的是 yum install net-snmp-python
    项目是
    http://net-snmp.sourceforge.net/download.html
    可以试试编译安装
    如果不行还是换 linux 吧

  8. 大佬我是小白 能教下么 完全不懂 给个联系方式啊

Leave a Reply

Your email address will not be published. Required fields are marked *