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-8import systry: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)}defwalk(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 errorIndicationreturn genericentaddr = 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)