Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. #!/usr/local/bin/python
  2.  
  3. try:
  4. # For Python 3.0 and later
  5. from urllib.request import urlopen
  6. except ImportError:
  7. # Fall back to Python 2's urllib2
  8. from urllib2 import urlopen
  9.  
  10. from jsonrpclib import Server
  11. import json
  12. import sys
  13. import getopt
  14. import ssl
  15. import time
  16. import socket
  17. import errno
  18. from datetime import datetime
  19. from pprint import pprint
  20. import xmlrpclib
  21. from multiprocessing.dummy import Pool as ThreadPool
  22.  
  23.  
  24. try:
  25. _create_unverified_https_context = ssl._create_unverified_context
  26. except AttributeError:
  27. # Legacy Python that doesn't verify HTTPS certificates by default
  28. pass
  29. else:
  30. # Handle target environment that doesn't support HTTPS verification
  31. ssl._create_default_https_context = _create_unverified_https_context
  32.  
  33. def json_from_url(url):
  34. response = urlopen(url)
  35. data = response.read().decode("utf-8")
  36. return json.loads(data)
  37.  
  38. def showVersion(switch):
  39. username = "*****"
  40. password = "*****"
  41. connection = Server( "https://" + username + ":" + password + "@" + switch + "/command-api" )
  42. print switch
  43. try:
  44. response = connection.runCmds( 1, ["show version"] )
  45. except socket.error,ERR:
  46. ErrorCode = ERR[0]
  47. if ErrorCode == errno.ECONNREFUSED:
  48. return '{0:50} {1:40}'.format( switch, "[Error: Connection refused]" )
  49. elif ErrorCode == errno.EHOSTUNREACH:
  50. return '{0:50} {1:40}'.format( switch, "[Error: No route to host]" )
  51. elif ErrorCode == errno.ECONNRESET:
  52. return '{0:50} {1:40}'.format( switch, "[Error: Connection reset]" )
  53. else:
  54. # Unknwon error - report number and error string (should capture all)
  55. return'{0:50} {1:40}'.format( switch, "[Error: " + str(ErrorCode) + " " + ERR[1] + "]" )
  56. #raise ERR;
  57. except xmlrpclib.ProtocolError as err:
  58. return '{0:50} {1:40}'.format( switch, "[Error: " + str(err.errcode) + " " + str(err.errmsg) + "]" )
  59. else:
  60. return '{0:50} {1:40}'.format( switch, str(response[0]['version']) )
  61.  
  62. def getDevicesFromPortal():
  63. netdbDevices = json_from_url( "https://portal.tower-research.com/portal/api/v1/netdb/device/?username=netops-api&amp=&api_key=2127c497de605b24253cfc7ffd02f5524f85cf33&limit=0&offset=0" )
  64. count = 0
  65. switches = []
  66. while count <= netdbDevices['meta']['total_count']:
  67. if netdbDevices['objects'][count]['vendor']['name'] == 'Arista' and netdbDevices['objects'][count]['status'] == 1:
  68. switches.append( netdbDevices['objects'][count]['name'] + "." + netdbDevices['objects'][count]['site']['dns_prefix'] )
  69. count += 1
  70. return switches
  71.  
  72.  
  73.  
  74. def main():
  75. startTime = datetime.now()
  76. switches = getDevicesFromPortal()
  77. pool = ThreadPool(100)
  78. output = pool.map( showVersion, switches )
  79. pool.close()
  80. pool.join()
  81. for each in range( 1, 10 ):
  82. print ""
  83. for each in output:
  84. print each
  85. print datetime.now() - startTime
  86.  
  87. if __name__ == '__main__':
  88. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement