当前位置:   article > 正文

python利用pysnmp获取交换机级联接口_python 检测 交换机 trunk 端口

python 检测 交换机 trunk 端口

交换机里有一个arp表,利用arp表可以找到与其相连的交换机的mac地址,然后再从mac端口对应表里找出端口


  1. def getTrunk(ip,port,agent,communication):
  2. macAddr = []
  3. macList = []
  4. portList = []
  5. macStrList = []
  6. linkPort = []
  7. oid1 = (1,3,6,1,2,1,4,22,1,2)#ARP表oid
  8. oid2 = (1,3,6,1,2,1,17,4,3,1,2)#mac端口对应表lid
  9. gen = cmdgen.CommandGenerator()
  10. errorIndication, errorStatus, errorIndex, varBinds = gen.nextCmd(
  11. cmdgen.CommunityData(agent, communication,1),
  12. cmdgen.UdpTransportTarget((ip,port)),
  13. oid1,
  14. )
  15. #获取ARP表里的mac地址
  16. for varBind in varBinds:
  17. for name,val in varBind:
  18. macAddr.append(val.prettyPrint(0))
  19. gen.ignoreNonIncreasingOid = True#让oid可以非递增
  20. errorIndication, errorStatus, errorIndex, varBinds = gen.nextCmd(
  21. cmdgen.CommunityData(agent, communication,1),
  22. cmdgen.UdpTransportTarget((ip,port)),
  23. oid2,
  24. )
  25. #获取mac端口对应关系
  26. for varBind in varBinds:
  27. for name,val in varBind:
  28. macList.append(name[-6:])
  29. portList.append(val.prettyPrint())
  30. #将十进制mac地址转化为16进制的字符串
  31. for item in macList:
  32. temp = ''
  33. for part in item:
  34. temp += str(hex(int(part)))[2:]
  35. temp = '0x' + temp
  36. macStrList.append(temp)
  37. for item in macAddr:#获取端口号
  38. if item in macStrList:
  39. index = macStrList.index(item)
  40. linkPort.append(portList[index])
  41. #去除列表中的重复元素
  42. linkPort = {}.fromkeys(linkPort).keys()#linkPort = list(set(linkPort))
  43. return linkPort


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/156709
推荐阅读
相关标签
  

闽ICP备14008679号