当前位置:   article > 正文

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

python通过snmp采集思科交换机

首先使用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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/156755
推荐阅读
相关标签
  

闽ICP备14008679号