/backend/hosttracking/getmacs.py

https://github.com/BillTheBest/FooNMS · Python · 75 lines · 55 code · 7 blank · 13 comment · 8 complexity · c2dacd73359d60fc5e608bacee933c20 MD5 · raw file

  1. #!/usr/bin/python
  2. #http://pysnmp.sourceforge.net/docs/4.x/index.html#SYNCH-ONELINER-APPS
  3. #includes
  4. from pysnmp.entity.rfc3413.oneliner import cmdgen
  5. # functions
  6. def hexify( octets ):
  7. return ":".join( [ '%x'%(ord(c)) for c in octets ]) #http://www.velocityreviews.com/forums/t320222-octet-string-conversion.html
  8. def grabmacs (host,community): #gets a list of mac addresses, returns a dict of macid and mac address
  9. errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().nextCmd(
  10. cmdgen.CommunityData('my-agent', community, 0),
  11. cmdgen.UdpTransportTarget((host, 161)),
  12. (1,3,6,1,2,1,17,4,3,1,1))
  13. table = {}
  14. #http://www.cisco.com/en/US/tech/tk648/tk362/technologies_tech_note09186a00801c9199.shtml
  15. for varBind in varBinds:
  16. macid, mac = varBind[0]
  17. table[str(macid.prettyPrint().split(".")[-6::])] = hexify(mac)
  18. return(table)
  19. def grabbridgeports (host, community): #grabs a list of macid and port numbers. returns a dict of macid and portid
  20. errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().nextCmd(
  21. cmdgen.CommunityData('my-agent', community, 0),
  22. cmdgen.UdpTransportTarget((host, 161)),
  23. (1,3,6,1,2,1,17,4,3,1,2))
  24. table = {}
  25. #http://www.cisco.com/en/US/tech/tk648/tk362/technologies_tech_note09186a00801c9199.shtml
  26. for varBind in varBinds:
  27. macid, portid = varBind[0]
  28. table[str(macid.prettyPrint().split(".")[-6::])] = portid
  29. return(table)
  30. def grabportindex (host, community): #grabs a list of macid and port numbers. returns a dict of macid and portid
  31. errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().nextCmd(
  32. cmdgen.CommunityData('my-agent', community, 0),
  33. cmdgen.UdpTransportTarget((host, 161)),
  34. (1,3,6,1,2,1,17,1,4,1,2))
  35. table = {}
  36. #http://www.cisco.com/en/US/tech/tk648/tk362/technologies_tech_note09186a00801c9199.shtml
  37. for varBind in varBinds:
  38. index, portid = varBind[0]
  39. table[index.prettyPrint().split(".")[-1]] = portid
  40. return(table)
  41. def grabportnames (host,community): #grabs a list of portnames. returns a portid and a human name of the port as a dict
  42. errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().nextCmd(
  43. cmdgen.CommunityData('my-agent', community, 0),
  44. cmdgen.UdpTransportTarget((host, 161)),
  45. (1,3,6,1,2,1,31,1,1,1,1))
  46. table = {}
  47. #http://www.cisco.com/en/US/tech/tk648/tk362/technologies_tech_note09186a00801c9199.shtml
  48. for varBind in varBinds:
  49. portid, portname = varBind[0]
  50. table[portid.prettyPrint().split(".")[-1]] = portname
  51. return(table)
  52. def getmacs(host,community):
  53. macs = grabmacs(host,community)
  54. bridgeports = grabbridgeports(host,community)
  55. index = grabportindex(host,community)
  56. portnames = grabportnames(host,community)
  57. table = {}
  58. for mac in macs:
  59. if index == {}:
  60. table[macs[mac]] = portnames[str(bridgeports[mac])]
  61. else:
  62. table[macs[mac]] = portnames[str(index[str(bridgeports[mac])])]
  63. return(table)
  64. #testing
  65. #print grabmacs("172.27.2.1","public")
  66. #print grabbridgeports("172.27.2.1","public")
  67. #print grabportnames("172.27.2.1","public")
  68. # print getmacs("172.27.2.43","public")