#!/usr/bin/python3
# -*- coding=utf-8 -*-
from pysnmp.entity import engine, config
from pysnmp.entity.rfc3413 import cmdgen
from pysnmp.carrier.asynsock.dgram import udp
dic_intercer_dic={}
ip='IP地址'
user='zbx-net'
hash_key='秘钥(usmHMACSHAAuthProtocol)'
cry_key='密钥(usmAesCfb128Protocol)'
snmpEngine = engine.SnmpEngine()
config.addSocketTransport(
snmpEngine,
udp.domainName,
udp.UdpSocketTransport().openClientMode())
def cbFun1(sendRequesthandle, errorIndication, errorStatus, errorIndex,
varBindTable, cbCtx):
for varBindRow in varBindTable:
for oid, val in varBindRow:
if not oid.prettyPrint().startswith('1.3.6.1.2.1.31.1.1.1.18'): return False
k1=oid.prettyPrint().replace('1.3.6.1.2.1.31.1.1.1.18.','')
v1=val.prettyPrint().replace('Huawei, USG6670 Series,','').replace(' Interface','')
dic_intercer_dic[k1]=v1
return True
def snmpv3_getbulk(oid=''):
config.addTargetAddr(
snmpEngine, 'yourDevice',
udp.domainName, (ip, 161),
'my-creds' )
hashval = config.usmHMACSHAAuthProtocol
cryval = config.usmAesCfb128Protocol
model = 'authPriv'
config.addV3User(
snmpEngine, user,
hashval, hash_key,
cryval, cry_key)
config.addTargetParams(snmpEngine, 'my-creds', user, model)
cmdgen.BulkCommandGenerator().sendReq(
snmpEngine,
'yourDevice',
0, 25, # non-repeaters, max-repetitions
( (oid, None),),
cbFun1)
snmpEngine.transportDispatcher.runDispatcher()
snmpv3_getbulk('1.3.6.1.2.1.31.1.1.1.18') ##拿到接口的索引值,存入字典dic_intercer_dic
def cbFun2(sendRequesthandle, errorIndication, errorStatus, errorIndex,
varBindTable, cbCtx):
for varBindRow in varBindTable:
for oid, val in varBindRow:
if not oid.prettyPrint().startswith('1.3.6.1.2.1.4.24.4.1.5'): return False
data_ipinfo=oid.prettyPrint().replace('1.3.6.1.2.1.4.24.4.1.5.',' ')
data_ipinfo=data_ipinfo.split('.')
print('.'.join(data_ipinfo[0:4]),'.'.join(data_ipinfo[5:9]),'.'.join(data_ipinfo[9:]),dic_intercer_dic.get(val.prettyPrint()))
return True # signal dispatcher to continue walking
def snmpv3_getbulk2(oid=''):
config.addTargetAddr(
snmpEngine, 'yourDevice',
udp.domainName, (ip, 161),
'my-creds')
# usmHMACSHAAuthProtocol - SHA hashing
# usmNoAuthProtocol - no authentication
# usmDESPrivProtocol - DES encryption
# usm3DESEDEPrivProtocol - triple-DES encryption
# usmAesCfb128Protocol - AES encryption, 128-bit
# usmAesCfb192Protocol - AES encryption, 192-bit
# usmAesCfb256Protocol - AES encryption, 256-bit
# usmNoPrivProtocol - no encryption
hashval = config.usmHMACSHAAuthProtocol
cryval = config.usmAesCfb128Protocol
model = 'authPriv'
config.addV3User(
snmpEngine, user,
hashval, hash_key,
cryval, cry_key
)
config.addTargetParams(snmpEngine, 'my-creds', user, model)
cmdgen.BulkCommandGenerator().sendReq(
snmpEngine,
'yourDevice',
0, 25, # non-repeaters, max-repetitions
( (oid, None),),
cbFun2)
snmpEngine.transportDispatcher.runDispatcher()
snmpv3_getbulk2('1.3.6.1.2.1.4.24.4.1.5') ##查看路由信息,结合端口显示出来
'''
目的地址 掩码 下一条 出接口
10.xx.114.0 255.255.0.0 10.xx.8.50 Eth-Trunk8.3207
10.xx.115.0 255.255.0.0 10.xx.8.50 Eth-Trunk8.3207
10.xx.116.0 255.255.0.0 10.xx.8.50 Eth-Trunk8.3207
'''