Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/python
- try:
- # For Python 3.0 and later
- from urllib.request import urlopen
- except ImportError:
- # Fall back to Python 2's urllib2
- from urllib2 import urlopen
- from jsonrpclib import Server
- import json
- import sys
- import getopt
- import ssl
- import time
- import socket
- import errno
- from datetime import datetime
- from pprint import pprint
- import xmlrpclib
- from multiprocessing.dummy import Pool as ThreadPool
- try:
- _create_unverified_https_context = ssl._create_unverified_context
- except AttributeError:
- # Legacy Python that doesn't verify HTTPS certificates by default
- pass
- else:
- # Handle target environment that doesn't support HTTPS verification
- ssl._create_default_https_context = _create_unverified_https_context
- def json_from_url(url):
- response = urlopen(url)
- data = response.read().decode("utf-8")
- return json.loads(data)
- def showVersion(switch):
- username = "*****"
- password = "*****"
- connection = Server( "https://" + username + ":" + password + "@" + switch + "/command-api" )
- print switch
- try:
- response = connection.runCmds( 1, ["show version"] )
- except socket.error,ERR:
- ErrorCode = ERR[0]
- if ErrorCode == errno.ECONNREFUSED:
- return '{0:50} {1:40}'.format( switch, "[Error: Connection refused]" )
- elif ErrorCode == errno.EHOSTUNREACH:
- return '{0:50} {1:40}'.format( switch, "[Error: No route to host]" )
- elif ErrorCode == errno.ECONNRESET:
- return '{0:50} {1:40}'.format( switch, "[Error: Connection reset]" )
- else:
- # Unknwon error - report number and error string (should capture all)
- return'{0:50} {1:40}'.format( switch, "[Error: " + str(ErrorCode) + " " + ERR[1] + "]" )
- #raise ERR;
- except xmlrpclib.ProtocolError as err:
- return '{0:50} {1:40}'.format( switch, "[Error: " + str(err.errcode) + " " + str(err.errmsg) + "]" )
- else:
- return '{0:50} {1:40}'.format( switch, str(response[0]['version']) )
- def getDevicesFromPortal():
- netdbDevices = json_from_url( "https://portal.tower-research.com/portal/api/v1/netdb/device/?username=netops-api&=&api_key=2127c497de605b24253cfc7ffd02f5524f85cf33&limit=0&offset=0" )
- count = 0
- switches = []
- while count <= netdbDevices['meta']['total_count']:
- if netdbDevices['objects'][count]['vendor']['name'] == 'Arista' and netdbDevices['objects'][count]['status'] == 1:
- switches.append( netdbDevices['objects'][count]['name'] + "." + netdbDevices['objects'][count]['site']['dns_prefix'] )
- count += 1
- return switches
- def main():
- startTime = datetime.now()
- switches = getDevicesFromPortal()
- pool = ThreadPool(100)
- output = pool.map( showVersion, switches )
- pool.close()
- pool.join()
- for each in range( 1, 10 ):
- print ""
- for each in output:
- print each
- print datetime.now() - startTime
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement