当前位置:   article > 正文

Python:通过SNMP协议获取华为交换机的ARP地址表_不知道oid 可以通过snmp获取交换机的arp信息吗

不知道oid 可以通过snmp获取交换机的arp信息吗

华为交换机SNMP配置

system-view 进入交换机的配置模式、
[switch]snmp-agent community read huawei 配置community 只读属性为huawei
[switch]snmp-agent community write huawei 配置community 可写属性为huawei
[switch]snmp-agent sys-info version all 配置版本为所有

# coding=utf-8

import sys

try:
    from pysnmp.entity.rfc3413.oneliner import cmdgen
except Exception as e:
    print("You need to download pysnmp and pyasn1", e)
    sys.exit(1)

oTable = {
    "entLogicalCommunity": (1, 3, 6, 1, 2, 1, 47, 1, 2, 1, 1, 4),
    "entPhysicalModelName": (1, 3, 6, 1, 2, 1, 47, 1, 1, 1, 1, 13, 1),
    "entLogicalDescr": (1, 3, 6, 1, 2, 1, 47, 1, 2, 1, 1, 2),
    "dot1dBasePort": (1, 3, 6, 1, 2, 1, 17, 1, 4, 1, 1),
    "dot1dTpFdbPort": (1, 3, 6, 1, 2, 1, 17, 4, 3, 1, 2),
    "dot1dBasePortIfIndex": (1, 3, 6, 1, 2, 1, 17, 1, 4, 1, 2),
    "dot1dTpFdbAddress": (1, 3, 6, 1, 2, 1, 17, 4, 3, 1, 1),
    "ifDescr": (1, 3, 6, 1, 2, 1, 2, 2, 1, 2),
    "ifName": (1, 3, 6, 1, 2, 1, 31, 1, 1, 1, 1),
    "ifSpeed": (1, 3, 6, 1, 2, 1, 2, 2, 1, 5),
    "ifAlias": (1, 3, 6, 1, 2, 1, 31, 1, 1, 1, 18),
    "sysName": (1, 3, 6, 1, 2, 1, 1, 5, 0),
    "sysDescr": (1, 3, 6, 1, 2, 1, 1, 1, 0),
    "dot3StatsDuplexStatus": (1, 3, 6, 1, 2, 1, 10, 7, 2, 1, 19),
    "ifAdminStatus": (1, 3, 6, 1, 2, 1, 2, 2, 1, 7),
    "ifOperStatus": (1, 3, 6, 1, 2, 1, 2, 2, 1, 8),
    "atPhysAddress": (1, 3, 6, 1, 2, 1, 3, 1, 1, 2),
    "ipAdEntAddr": (1, 3, 6, 1, 2, 1, 4, 20, 1, 1),
    "ipAdEntIfIndex": (1, 3, 6, 1, 2, 1, 4, 20, 1, 2),
    "ARP": (1, 3, 6, 1, 2, 1, 3, 1, 1, 2),
    "HW": (1, 3, 6, 1, 2, 1, 4, 22, 1, 2)
}


def walk(dswitch, community, oid):
    """This function will return the table of OID's that I am walking"""
    errorIndication, errorStatus, errorIndex, \
    generic = cmdgen.CommandGenerator().nextCmd(cmdgen.CommunityData('my-agent', community, mpModel=1),
                                                cmdgen.UdpTransportTarget((dswitch, 161)), oid)
    if errorIndication:
        return errorIndication
    return generic


entaddr = walk("192.168.80.1", "huawei", oTable["HW"])

arp_table = []


"""======主函数======"""
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    for i in entaddr:
        temp = ''
        for j in i:
            temp += str(j)
        temp = temp.replace("SNMPv2-SMI::mib-2.4.22.1.2.", "")
        temp = temp[temp.find('.') + 1:]
        temp = temp.split(' = ')
        arp_table.append(temp)

    # 打印ARP表
    for list_IP_MAC in arp_table:
        print(list_IP_MAC)



  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/156749
推荐阅读
相关标签
  

闽ICP备14008679号