def load_cvp_data(): clnt = CvpClient() clnt.connect([cvp_ip], cvp_user, cvp_password) api = CvpApi(clnt) leaf_inventory = api.get_devices_in_container('LEAF') leaf_fqdn_list = list() for element in leaf_inventory: hostname = re.split('\.', element['fqdn'])[0] leaf_fqdn_list.append(hostname) return leaf_fqdn_list
def __init__(self, logger='cvprac', syslog=False, filename=None, log_level='INFO'): ''' Initialize the client and configure logging. Either syslog, file logging, both, or none can be enabled. If neither syslog nor filename is specified then no logging will be performed. Args: logger (str): The name assigned to the logger. syslog (bool): If True enable logging to syslog. Default is False. filename (str): Log to the file specified by filename. Default is None. log_level (str): Log level to use for logger. Default is INFO. ''' self.apiversion = None self.authdata = None self.cert = False self.connect_timeout = None self.cookies = None self.error_msg = '' self.node_cnt = None self.node_pool = None self.nodes = None self.port = None self.protocol = None self.session = None self.url_prefix = None self.url_prefix_short = None self.is_cvaas = False self.tenant = None self.cvaas_token = None self.api_token = None self.version = None self._last_used_node = None # Save proper headers self.headers = { 'Accept': 'application/json', 'Content-Type': 'application/json' } self.log = logging.getLogger(logger) self.set_log_level(log_level) if syslog: # Enables sending logging messages to the local syslog server. self.log.addHandler(SysLogHandler()) if filename: # Enables sending logging messages to a file. self.log.addHandler(logging.FileHandler(filename)) if syslog is False and filename is None: # Not logging so use the null handler self.log.addHandler(logging.NullHandler()) # Instantiate the CvpApi class self.api = CvpApi(self)
def add_configlet(configlet_name, configlet_string, switch_list): clnt = CvpClient() clnt.connect([cvp_ip], cvp_user, cvp_password) api = CvpApi(clnt) api.add_configlet(configlet_name, configlet_string) for switch_name in switch_list: switch_id = api.get_device_by_name(switch_name) configlet_id = api.get_configlet_by_name(configlet_name) api.apply_configlets_to_device('3rd-party', switch_id, [configlet_id], create_task=True) return 'Ignore'
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) ssl._create_default_https_context = ssl._create_unverified_context parser = argparse.ArgumentParser() parser.add_argument('--username', required=True) parser.add_argument('--cvpip', required=True) args = parser.parse_args() switchuser = args.username cvpip = args.cvpip switchpass = getpass() clnt = CvpClient() clnt.connect([cvpip], switchuser, switchpass) clntapi = CvpApi(clnt) inventory = clntapi.get_inventory() d1 = time.strftime("%Y_%m_%d_%H_%M_%S", time.gmtime()) os.mkdir(d1) for device in inventory: hostname = device["hostname"] print(hostname) device_mac = device["systemMacAddress"] runningConfig = clntapi.get_device_configuration(device_mac) filename = hostname + "_show_run_" + d1 + ".txt" with open(d1 + "/" + filename, 'w') as f: f.write(runningConfig)
from pprint import pprint from cvprac.cvp_client import CvpClient from cvprac.cvp_api import CvpApi import urllib3 import time urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning ) # This will disable invalid cert warnings client = CvpClient() client.connect(['cvp.lab.local'], 'jpatterson', 'P3pp3r101!') cvp = CvpApi(client) # test = cvp.get_cvp_info() # print test # # gettask = cvp.get_tasks() # pprint(gettask) '''Get inventory of CVP, then strip out FQDN of hostname and present just hostname:''' # host_list = [] # def hostname(filter=''): # inventory = cvp.get_inventory(query=filter) # full_fqdn = 'lab.local' # for value in inventory: # if full_fqdn in value['fqdn']: # fqdn_strip = value['fqdn'][0:-10] # host_list.append(fqdn_strip) # return host_list # # hostname()
parser.add_argument('--cvpIP', required=True) parser.add_argument('--containertobemoved', required=True) parser.add_argument('--targetcontainer', required=True) parser.add_argument('--suffixremove', required=True) args = parser.parse_args() switchuser = args.username cvpIP = args.cvpIP switchpass = getpass() containertobemoved = args.containertobemoved targetcontainer = args.targetcontainer suffixremove = args.suffixremove clnt = CvpClient() clnt.connect([cvpIP], switchuser, switchpass) clntapi = CvpApi(clnt) app_name = "" getContainers = clntapi.get_containers()["data"] tree = Tree() tree.create_node("Tenant", "Tenant") #root for container in getContainers: containername = container["name"] parentName = container["parentName"] parentId = container["parentId"] if containername != "Tenant": if tree.contains(parentName): if tree.contains(containername) is False: tree.create_node(containername,
from pprint import pprint from cvprac.cvp_client import CvpClient from cvprac.cvp_api import CvpApi import urllib3 import requests urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning ) # This will disable invalid cert warnings client = CvpClient() api = CvpApi(client) client.connect(['cvp.lab.local'], 'jpatterson', 'P3pp3r101!') result = client.get('/cvpInfo/getCvpInfo.do') newresult = client.get( 'https://cvp.lab.local/cvpservice/image/getImages.do?startIndex=0&endIndex=0' ) print newresult # test1 = clnt.get('/cvpservice/snapshot/getSnapshots.do?startIndex=0&endIndex=0') print result