get-sw-inv.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/usr/bin/env python2
  2. #
  3. # Copyright (c) 2017-2018 Joe Clarke <jclarke@cisco.com>
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. # 1. Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. # notice, this list of conditions and the following disclaimer in the
  13. # documentation and/or other materials provided with the distribution.
  14. #
  15. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. # SUCH DAMAGE.
  26. import requests
  27. import json
  28. import sys
  29. import time
  30. import os
  31. import re
  32. from subprocess import call
  33. PI_USER = ''
  34. PI_PASS = ''
  35. PI = '10.66.200.11'
  36. PAGE_SIZE = 1000
  37. def get_devs():
  38. global PI_USER, PI_PASS, PI, PAGE_SIZE, room_id, SPARK_API, SPARK_TOKEN, TEXT_BAD, TEXT_GOOD
  39. url = "https://%s/webacs/api/v1/data/InventoryDetails.json?.full=true&.maxResults=%d" % (
  40. PI, PAGE_SIZE)
  41. headers = {
  42. 'Connection': 'close'
  43. }
  44. devices = []
  45. dets = []
  46. done = False
  47. first = 0
  48. while not done:
  49. code = 401
  50. i = 0
  51. nurl = url + "&.firstResult=" + str(first * PAGE_SIZE)
  52. while code != 200 and i < 40:
  53. response = requests.request("GET", nurl, auth=(
  54. PI_USER, PI_PASS), headers=headers, verify=False)
  55. code = response.status_code
  56. if code != 200:
  57. i += 1
  58. time.sleep(3)
  59. if code == 200:
  60. j = json.loads(response.text)
  61. if int(j['queryResponse']['@last']) + 1 == int(j['queryResponse']['@count']):
  62. done = True
  63. else:
  64. first += 1
  65. for dev in j['queryResponse']['entity']:
  66. det_dic = {}
  67. if 'ipAddress' not in dev['inventoryDetailsDTO']['summary']:
  68. continue
  69. det_dic['ip'] = dev['inventoryDetailsDTO'][
  70. 'summary']['ipAddress']
  71. det_dic['serial'] = 'N/A'
  72. if 'udiDetails' in dev['inventoryDetailsDTO']:
  73. for udi in dev['inventoryDetailsDTO']['udiDetails']['udiDetail']:
  74. if 'productId' in udi:
  75. if re.search(r'^WS-C', udi['productId']):
  76. det_dic['serial'] = udi['udiSerialNr']
  77. dets.append(det_dic)
  78. first = 0
  79. done = False
  80. url = "https://%s/webacs/api/v1/data/Devices.json?.full=true&.maxResults=%d" % (
  81. PI, PAGE_SIZE)
  82. while not done:
  83. code = 401
  84. i = 0
  85. nurl = url + "&.firstResult=" + str(first * PAGE_SIZE)
  86. while code != 200 and i < 40:
  87. response = requests.request("GET", nurl, auth=(
  88. PI_USER, PI_PASS), headers=headers, verify=False)
  89. code = response.status_code
  90. if code != 200:
  91. i += 1
  92. time.sleep(3)
  93. if code == 200:
  94. j = json.loads(response.text)
  95. if int(j['queryResponse']['@last']) + 1 == int(j['queryResponse']['@count']):
  96. done = True
  97. else:
  98. first += 1
  99. for dev in j['queryResponse']['entity']:
  100. dev_dic = {}
  101. if 'ipAddress' not in dev['devicesDTO']:
  102. continue
  103. if 'deviceName' in dev['devicesDTO']:
  104. dev_dic['name'] = dev['devicesDTO']['deviceName']
  105. else:
  106. dev_dic['name'] = dev['devicesDTO']['ipAddress']
  107. dev_dic['ip'] = dev['devicesDTO']['ipAddress']
  108. if 'deviceType' not in dev['devicesDTO']:
  109. continue
  110. dev_dic['serial'] = 'N/A'
  111. for det in dets:
  112. if det['ip'] == dev_dic['ip']:
  113. dev_dic['serial'] = det['serial']
  114. dev_dic['type'] = dev['devicesDTO']['deviceType']
  115. devices.append(dev_dic)
  116. return devices
  117. devs = get_devs()
  118. print "Name,IP,Type,Serial Number"
  119. for dev in devs:
  120. print dev['name'] + "," + dev['ip'] + "," + dev['type'] + "," + dev['serial']