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