Python_hwfw-snmpv3获取路由表信息

13 篇文章 0 订阅

#!/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
'''

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值