Guest User

Untitled

a guest
Dec 10th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from pysnmp.entity.rfc3413.oneliner import cmdgen
  4. import re
  5. import struct
  6. import getopt
  7. import sys
  8. import signal
  9.  
  10. OID_BRIDGE=(1,3,6,1,2,1,17,1,4,1,2)
  11. OID_ALIAS=(1,3,6,1,2,1,31,1,1,1,1)
  12. OID_DESC=(1,3,6,1,2,1,31,1,1,1,18)
  13.  
  14. SNMP_AGENT=""
  15. SNMP_COMMUNITY="public"
  16.  
  17. def signal_handler(signal, frame):
  18. sys.exit(0)
  19.  
  20. def is_integer(i):
  21. try:
  22. int(i)
  23. return True
  24. except ValueError:
  25. return False
  26.  
  27. def get_value_of_oid(oid, index):
  28. theOid=list(oid)
  29. theOid.append(index)
  30. errorIndication, errorStatus, \
  31. errorIndex, varBinds = cmdgen.CommandGenerator().getCmd(
  32. cmdgen.CommunityData('test-agent', SNMP_COMMUNITY),
  33. cmdgen.UdpTransportTarget((SNMP_AGENT, 161)),
  34. tuple(theOid)
  35. )
  36. if errorIndication:
  37. print "ERROR",errorIndication
  38. else:
  39. if errorStatus:
  40. print '%s ERROR at %s\n' % (
  41. errorStatus.prettyPrint(),
  42. errorIndex and varBinds[int(errorIndex)-1] or '?'
  43. )
  44. else:
  45. for name, val in varBinds:
  46. return val.prettyPrint()
  47.  
  48. def list_cam():
  49. errorIndication, errorStatus, errorIndex, varBindTable = cmdgen.CommandGenerator().nextCmd(cmdgen.CommunityData('test-agent', SNMP_COMMUNITY),cmdgen.UdpTransportTarget((SNMP_AGENT, 161)),(1,3,6,1,2,1,17,7,1,2,2,1,2))
  50. if errorIndication:
  51. print errorIndication
  52. else:
  53. if errorStatus:
  54. print '%s at %s\n' % (
  55. errorStatus.prettyPrint(),
  56. errorIndex and varBindTable[-1][int(errorIndex)-1] or '?'
  57. )
  58. else:
  59. for varBindTableRow in varBindTable:
  60. for name, val in varBindTableRow:
  61. tableau=re.findall("\d+", name.prettyPrint())[::-1]
  62. mac=(tableau[5],tableau[4],tableau[3],tableau[2],tableau[1],tableau[0])
  63. myBridge=get_value_of_oid(OID_BRIDGE,int(val.prettyPrint()))
  64. if (is_integer(myBridge)):
  65. ifAlias=get_value_of_oid(OID_ALIAS,int(myBridge))
  66. ifDescription=get_value_of_oid(OID_DESC,int(myBridge))
  67. print "%0.2x:%0.2x:%0.2x:%0.2x:%0.2x:%0.2x -> %s | %s" % (int(mac[0]), int(mac[1]), int(mac[2]), int(mac[3]), int(mac[4]), int(mac[5]), ifAlias, ifDescription)
  68.  
  69. def main():
  70. global SNMP_AGENT
  71. global SNMP_COMMUNITY
  72. try:
  73. opts, args = getopt.getopt(sys.argv[1:], 'h:c:', ["host", "community"])
  74. except getopt.GetoptError, err:
  75. print str(err)
  76. sys.exit(1)
  77.  
  78. for opt, arg in opts:
  79. if opt in ("-h", "--host"):
  80. SNMP_AGENT = arg
  81. print "Host:\t",arg
  82. elif opt in ("-c", "--community"): SNMP_COMMUNITY = arg
  83.  
  84. if len(SNMP_AGENT)!=0:
  85. list_cam()
  86. else:
  87. print "Please define the host with -h or --host"
  88. sys.exit(1)
  89.  
  90. if (__name__ == "__main__"):
  91. main()
Add Comment
Please, Sign In to add comment