def setUp(self): dir_name = os.path.dirname(os.path.abspath(__file__)) mfc = MockFuncClass() self.testbed = load( os.path.join(dir_name, 'mock_testbeds/testbed.yaml')) Blitz.parameters = ParameterMap() Blitz.uid = 'test.dev' Blitz.parameters['testbed'] = self.testbed Blitz.parameters['save_variable_name'] = { 'dev_name': 'PE2', 'command': 'sh version', 'sub_command': 'interface', 'type_k': 1500, 'list_item': [177, 24, 13, 45], 'iter_class': mfc, 'dict1': { 'st': "name" } } self.blitz_obj = Blitz() self.uid = self.blitz_obj.uid self.blitz_obj.parent = self self.blitz_obj.parent.parameters = mock.Mock() self.blitz_obj.parameters['test_sections'] = [{ 'section1': [{ 'execute': { 'command': 'cmd', 'device': 'PE1' } }] }] sections = self.blitz_obj._discover() self.section = sections[0].__testcls__(sections[0])
def setUp(self): dir_name = os.path.dirname(os.path.abspath(__file__)) f, self.jobfile = tempfile.mkstemp() init_runtime(runtime) runtime.configuration.load() runtime.job = Job(jobfile = self.jobfile, runtime = runtime, **runtime.configuration.components.job) mgr = runtime.tasks task = mgr.Task(testscript = os.path.join(dir_name, 'mock_yamls/trigger_datafile.yaml'), taskid = 'awesome') self.testbed = load(os.path.join(dir_name, 'mock_testbeds/testbed.yaml')) self.mock_testbed_devices() Blitz.parameters = ParameterMap() Blitz.uid = 'test.dev' Blitz.parameters['testbed'] = self.testbed self.blitz_obj = Blitz() self.uid = self.blitz_obj.uid self.blitz_obj.parent = self self.blitz_obj.parent.parameters = mock.Mock() self.dev = Device( name='PE1', os='iosxe') self.dev.custom = {'abstraction': {'order': ['os']}} self.blitz_obj.parameters['test_sections'] = [{'section1': [{'execute': {'command': 'cmd', 'device': 'PE1'}}]}] sections = self.blitz_obj._discover() self.kwargs = {'self': self.blitz_obj, 'testbed': self.testbed, 'section': sections[0].__testcls__(sections[0]), 'name': ''}
def main(): #load testbed testbed = load('testbed.yaml') #connect and suppress output testbed.connect(log_stdout=False) #use pcall to execute on all devices in parallel pcall(get_ospf, hostname=testbed.devices, dev=testbed.devices.values())
def __init__(self, **kwargs): self.__dict__.update(kwargs) structure = { 'devices': { self.device: { 'type': 'router', 'os': self.os, 'credentials': { 'default': { 'username': '******', 'password': '******', 'enable_password': '******' }, }, 'connections': { 'default': { 'protocol': 'ssh', 'ip': self.ip, }, }, } } } testbed = load(structure) self.terminal = testbed.devices[self.device] try: self.terminal.connect(init_exec_commands=[], init_config_commands=[], log_stdout=False) except ConnectionError: raise CriticalPingCheck( f'SSH timed out while trying to connect to {self.device} | rtmin=0;;;; rtavg=0;;;; rtmax=0;;;; pl=100;;;;' )
def get_device_auth_sessions(device_ip: str): ''' This function will retrieve the active authentication sessions on a given NAD/Switch (required: NAD's ip address), and returns a list of authentication sessions. ''' print('Verifying IP Address validity') try: ip = IPAddress(device_ip) print(f'The IP address is {ip.format()}') except: print('Error, invalid device IP address') return("ERROR: Invalid IP address") testbed_input = testbed_template testbed_input['devices']['device']['connections']['cli']['ip'] = ip.format() testbed = load(testbed_input) device = testbed.devices['device'] try: device.connect(via='cli', learn_hostname=True) except: print(f"ERROR: Problem connecting to {device_ip}...") return([f"ERROR: Problem connecting to {device_ip}..."]) try: auth_sessions = device.parse('show authentication sessions') except SchemaEmptyParserError: print(f"ERROR: No authentication sessions on {device_ip}.") return([f"ERROR: No authentication sessions on {device_ip}."]) except: print(f"ERROR: Problem parsing information from {device_ip}.") return([f"ERROR: Problem parsing information from {device_ip}."]) relevant_sessions = [] for interface in auth_sessions['interfaces']: for client in auth_sessions['interfaces'][interface]['client']: if auth_sessions['interfaces'][interface]['client'][client]['domain'] != "UNKNOWN": auth_details = device.parse( f"show authentication sessions interface {interface} details") session = {'Interface': interface, 'EndpointMAC': client, 'Status': auth_sessions['interfaces'][interface]['client'][client]['status'], 'Method': auth_sessions['interfaces'][interface]['client'][client]['method'], 'Username': auth_details['interfaces'][interface]['mac_address'][client]['user_name'], 'IPv4': auth_details['interfaces'][interface]['mac_address'][client]['ipv4_address'], 'NICVendor': EUI(client).oui.registration()['org'] } try: if "local_policies" in auth_details['interfaces'][interface]['mac_address'][client]: session['Vlan']: auth_details['interfaces'][interface]['mac_address'][client]['local_policies']['vlan_group']['vlan'] if "server_policies" in auth_details['interfaces'][interface]['mac_address'][client]: server_policies = auth_details['interfaces'][interface]['mac_address'][client]['server_policies'] for policy in server_policies: if server_policies[policy]['name'] == 'SGT Value': session['SGT'] = server_policies[policy]['policies'] elif server_policies[policy]['name'] == 'ACS ACL IPV6': session['IPv6ACL'] = server_policies[policy]['policies'] elif server_policies[policy]['name'] == 'ACS ACL': session['IPv4ACL'] = server_policies[policy]['policies'] except: pass relevant_sessions.append(session) return(relevant_sessions)
def main(): # Instantiate the Testbed testbed = load(arguments.testbed_file) print(f"\n======= TESTBED INFO =======\n") print(f"\tTestbed Value (object): {testbed}") # Use the dir method to check what options are available for this object # Uncomment to see output print(dir(testbed)) print(f"\tTestbed Name: \n\t\t{testbed.name}") print(f"\tTestbed Devices: \n\t\t{testbed.devices}") print(f"\tNumber of Testbed Links: \n\t\t{testbed.links}") print(f"\tNumber of Testbed Devices: \n\t\t{len(testbed.devices)}") print(f"\n======= END TESTBED INFO =======\n") # Using the default parameters and therefore the default Testbed if testbed.name == 'DevNet_Always_On_Sandbox_Devices': # Sandbox NXOS Device nx_dev, nx_resp = device_info('sbx-n9kv-ao', testbed, arguments.command, arguments.save) # csr1000v-1 csr_dev, csr_resp = device_info('csr1000v-1', testbed, arguments.command, arguments.save) else: # A non default testbed file has been provided # Example showing how to iterate over the devices in the testbed file for dev in testbed.devices: print(f"\n>>>>>>> DEVICE {dev}") dev, resp = device_info(dev, testbed, arguments.command, arguments.save)
def load_testbed(self, testbed): # Convert pyATS testbed to Genie Testbed logger.info( "Converting pyATS testbed to Genie Testbed to support pyATS Library features" ) testbed = load(testbed) self.parent.parameters.update(testbed=testbed)
def get_ospf_intf_state(self, dev, neighIntf, instance='1', area='0.0.0.0'): tb = load( '/home/dsalva/PycharmProjects/mse-cisco-R/mse-cisco/Resources/Source/InputFiles/testbed.yaml' ) dev = tb.devices['Automation-ASR9K-PE2'] dev.connect() attributes = [ 'info[vrf][(.*)][address_family][ipv4]' '[instance][(.*)]' '[areas][(.*)]' '[interfaces][(.*)]' ] ospf = dev.learn('ospf', attributes=attributes) intfState = ospf.info['vrf']['default']['address_family']['ipv4'][ 'instance'][instance]['areas'][area]['interfaces'][neighIntf].get( 'state') return intfState #op=get_ospf_neighState('66.66.66.66','State') #print(op) #op=get_ospf_neigh_state('GigabitEthernet0/0/0/1','66.66.66.66') #print("neighbor state:"+op) #op=get_ospf_intf_state('GigabitEthernet0/0/0/1') #print("intf state:"+op)
def connect_tb_devices(self, testbed): self.parent.parameters['testbed'] = testbed = load(testbed) # connect testbed devices for dev in testbed: dev.connect() dev.mdcli_execute("environment more false") logger.info('Device %s connected!' % dev.name)
def get_testbed(testbed): try: load_tb = load(testbed.strip()) return load_tb except TypeError: #This should catch an invaild testbed file e = sys.exc_info()[1] print(e) sys.exit(1)
def __init__(self, file_testbed): self.__file_testbed = file_testbed self.__testbed = testbed.load(file_testbed) self.__init_exec_commands = [] self.__log_stdout = False # self.__init_conf_commands = [] self.__lg = AkarLogging(logging.INFO, "TestbedConf") self.__logger = self.__lg.get_color_logger() self.__logger.info(f'Initialized class TestbedConf')
def get_current_config(self): #print("Getting Current Config") testbed_filename = "testbeds/" + self.options.testbed testbed = load(testbed_filename) TR = testbed.devices[self.options.hostname] TR.connect(log_stdout=False) output = TR.execute('show running-config') self.current_config = Config(output) self.current_config.tree()
def connect_devices(topology, form_dev_list): try: device_list = {} # connect to devices testbed = load(topology) temp_devices = testbed.devices devices = { x: y for (x, y) in temp_devices.items() if x in form_dev_list } for name in devices.keys(): device = devices[name] #attampt to connect to each device retries = 0 while retries < 3: try: if retries > 0: print(f'this is retry attempt #{retries}') device.connect(connection_timeout=5) device_list[name] = device break except Exception as e: print(f'Thumbs down connecting to device. Error Msg...{e}') if retries == 2: # Create a log when we are unable to connect to a device datetime_now = datetime.now() print(f'{datetime_now} -- ERROR -- {e}') f = open("/var/www/logs/errors.txt", "a") f.write(f'\n{datetime_now} -- ERROR -- {e}') f.close() f = open("/var/www/templates/error_log.html", "a") f.write( f'<br /><strong>{datetime_now}</strong> -- <span style="color:red">ERROR</span> -- {e}' ) f.close() time.sleep(5) retries = retries + 1 except Exception as e: # Get date and time datetime_now = datetime.now() print(f'{datetime_now} -- ERROR -- {e}') f = open("/var/www/logs/errors.txt", "a") f.write(f'\n{datetime_now} -- ERROR -- {e}') f.close() f = open("/var/www/templates/error_log.html", "a") f.write( f'<br /><strong>{datetime_now}</strong> -- <span style="color:red">ERROR</span> -- {e}' ) f.close() sys.exit("check error logs") return device_list
def main(): # Instantiate the Testbed testbed = load('my_testbed.yaml') print(f"\n======= TESTBED INFO =======\n") print(f"\tTestbed Value (object): {testbed}") print(f"\n======= END TESTBED INFO =======\n") device = testbed.devices['CISCOCSR'] device.connect() #device.execute('show version') response = device.parse('show version')
def mapping_datafile_creator(self, testbed): # Generating mapping datafile mapping_dict = {} mapping_dict.setdefault('devices', {}) testbed = load(testbed) for dev, dev_args in testbed.devices.items(): # if ha device mapping datafile with [a, b] if 'a' in dev_args.connections and \ 'b' in dev_args.connections: mapping_dict['devices'].update( {dev: { 'mapping': { 'cli': ['a', 'b'] } }}) # for single connection devices just a elif 'a' in dev_args.connections: mapping_dict['devices'].update( {dev: { 'mapping': { 'cli': 'a' } }}) # for devices with cli connection, # we pick the first connection in the list of connection # Usually these cases should have only one connection in the testbed else: connections = Dq(dev_args.connections).\ not_contains('default.*', regex=True).\ reconstruct() connection = list(connections.keys())[0] mapping_dict['devices'].update( {dev: { 'mapping': { 'cli': connection } }}) additionals_dir = self._get_dir_for_additional_datafile() with open(additionals_dir + '/mapping_datafile.yaml', 'w') as mapping_file_dumped: mapping_file_dumped.write( ruamel.yaml.round_trip_dump(mapping_dict)) return additionals_dir + '/mapping_datafile.yaml'
def connect_to_tb_devices(self, testbed): # convert a pyATS testbed to Genie testbed # genie testbed extends pyATS testbed and does more with it, eg, # adding .learn() and .parse() functionality # this step will be harmonized and no longer required in near future self.parent.parameters['testbed'] = testbed = load(testbed) # connect to device uut = testbed.devices['uut'] uut.connect() logger.info('We have made connectivity to device %s' % uut.name)
def connect_to_device(self, inputFile, device): dirPath = os.path.dirname(os.path.abspath(__file__)) dstFolder = os.path.dirname(os.path.abspath(dirPath)) filepath = dstFolder + "/InputFiles/" + inputFile tb = load(filepath) try: dev = tb.devices[device] dev.connect() except: LOG.info("Cannot connect to device: {0}".format(err)) sys.exit(1) return dev
def test_stage_not_configured(self): from genie.libs.clean.clean import CleanTestcase tb = load(test_path + '/mock_testbed.yaml') k = KleenexFileLoader(testbed=tb, invoke_clean=True).\ load(test_path+'/mock_clean_invalid.yaml') KleenexEngine.update_testbed(tb, **k['devices']) device = tb.devices['PE1'] tc = CleanTestcase(device, global_stage_reuse_limit=1) with self.assertRaisesRegex(AEtestFailedSignal, 'has no configuration in clean.yaml'): tc()
def setUp(self): # Load sample testbed YAML & clean YAML self.tb = load(test_path + '/mock_testbed.yaml') self.clean_config = KleenexFileLoader(testbed=self.tb, invoke_clean=True).\ load(test_path+'/mock_clean.yaml') KleenexEngine.update_testbed(self.tb, **self.clean_config['devices']) self.steps = Steps() self.device = self.tb.devices['N95'] self.device.is_ha = None self.raw_output = PassedStageOutputs self.section = TestItem(uid='test', description='', parameters={})
def connect(self, testbed): dev_list = {} #connect to devices testbed = load(testbed) devices = testbed.devices for name in devices.keys(): device = devices[name] try: device.connect(connection_timeout=5) except ConnectionError: log.info(f'Connection attempt failed to - {name}') continue dev_list[name] = device self.parent.parameters.update(dev=dev_list)
def clean(self, device, reporter, *args, **kwargs): # In this section we will convert to Genie Testbed testbed = load(device.testbed) device = testbed.devices[device.name] clean_testcase = CleanTestcase(device) clean_testcase.reporter = reporter.testcase(clean_testcase) with clean_testcase: # 1. Figure out what section to run # 2. Run them result = clean_testcase() if not result: raise Exception("Clean {result}.".format(result=str(result)))
def __init__(self, maple_file, new_yaml=None, testbed=None, testcase_control=None, teststep_control=None, tims_testplan_folder=None): self.maple_file = maple_file self.new_yaml = new_yaml self.uids = [] self.testbed = load(testbed) if testbed else runtime.testbed self.testcase_control = testcase_control self.teststep_control = teststep_control self.tims_testplan_folder = tims_testplan_folder
def connect(self, testbed): genie_testbed = load(testbed) self.parent.parameters['testbed'] = genie_testbed device_list = [] for device in genie_testbed.devices.values(): log.info(banner( f"Connect to device '{device.name}'")) try: device.connect() device_list.append(device) except Exception as e: log.info(f"Failed to establish connection to '{device.name}'") self.parent.parameters.update(dev=device_list) log.debug(self.parent.parameters)
def setUp(self): dir_name = os.path.dirname(os.path.abspath(__file__)) self.testbed = load(os.path.join(dir_name, 'mock_testbeds/testbed.yaml')) Blitz.parameters = ParameterMap() Blitz.uid = 'test.dev' Blitz.parameters['testbed'] = self.testbed self.blitz_obj = Blitz() self.dev = Device( name='PE1', os='iosxe') self.dev.custom = {'abstraction': {'order': ['os']}} self.blitz_obj.parameters['test_sections'] = [{'section1': [{'action': {'command': 'a'}}]}] sections = self.blitz_obj._discover() self.kwargs = {'self': self.blitz_obj, 'section': sections[0], 'name': ''}
def connect(self, testscript, testbed): """ Common Setup subsection """ log.info("Aetest Common Setup ") testbed = load('routerIOL_tb.yaml') routeriol1 = testbed.devices['routeriol'] routeriol2 = testbed.devices['routeriol2'] routeriol3 = testbed.devices['routeriol3'] routeriol1.connect() routeriol2.connect() routeriol3.connect() testscript.parameters['uut'] = routeriol1 testscript.parameters['uut2'] = routeriol2 testscript.parameters['uut3'] = routeriol3
def _initiate_blitz_cls(self, yaml_file): dir_name = os.path.dirname(os.path.abspath(__file__)) self.blitz_cls = Blitz self.testbed = load(os.path.join(dir_name, 'mock_testbeds/testbed.yaml')) self.blitz_cls.parameters = ParameterMap() self.blitz_cls.parameters['testbed'] = self.testbed self._mock_testbed_devs() self.datafile = yaml.safe_load(yaml_file) for key, value in self.datafile.items(): self.blitz_cls.uid = "{}.{}".format(key, value['devices'][0]) self.blitz_cls.parameters['test_sections'] = value['test_sections'] if value.get('description'): self.blitz_cls.description = value['description']
def main(): """ This first pyATS Genie script instantiates the devnet_sbx_testbed.yml Testbed file which has two DevNet Always On Sandbox devices. It then establishes a connection to each device and executes a show command ("show version"). All of this is hardcoded and there is lots of code repetition but this first script is intended to show the basics without alot of "extras" or flexibility. :return: """ # Instantiate the Testbed # testbed = load('devnet_sbx_testbed_secure.yml') testbed = load('devnet_sbx_testbed.yml') print(f"\n======= TESTBED INFO =======\n") print(f"\tTestbed Value (object): {testbed}") print(f"\n======= END TESTBED INFO =======\n") # Sandbox NXOS Device # CLI: genie parse "show version" --testbed-file "devnet_sbx_testbed.yml" --devices "sbx-n9kv-ao" # This CLI command outputs the results into a directory called "out1" which does not have to exist # CLI & SAVE: genie parse "show version" --testbed-file "devnet_sbx_testbed.yml" --devices "sbx-n9kv-ao" --output PRE # DIFF CLI: genie diff PRE POST device = testbed.devices['sbx-n9kv-ao'] # device = Connection(hostname='sbx-n9kv-ao', log_stdout=False) # print(dir(device)) device.connect() response = device.parse('show version') print( f"\nResponse from sbx-n9kv-ao is of type {type(response)} and length {len(response)}" ) print(f"\n== RAW Response: \n{response}") print() print(f"== Formatted Response: \n{json.dumps(response, indent=4)}") print(response.keys()) # csr1000v-1 IOS-XE # CLI: genie parse "show version" --testbed-file devnet_sbx_testbed.yml --devices "csr1000v-1" device = testbed.devices['csr1000v-1'] device.connect() response = device.parse('show version') print( f"\nResponse from csr1000v-1 is of type {type(response)} and length {len(response)}" ) print(f"\n== RAW Response: \n{response}") print() print(f"== Formatted Response: \n{json.dumps(response, indent=4)}") print(response.keys())
def get_ospf_neighState(index_value, field, index=0): tb = load( '/home/dsalva/PycharmProjects/mse-cisco-R/mse-cisco/Resources/Source/InputFiles/testbed.yaml' ) dev = tb.devices['Automation-ASR9K-PE2'] dev.connect() output = dev.device.execute('show ospf neighbor') header = [ 'Neighbor ID', 'Pri', 'State', 'Dead Time', 'Address', 'Interface' ] result = oper_fill_tabular(device_output=output, device_os='iosxr', header_fields=header, index=index) state = result.entries[index_value][field] dev.disconnect() return state
def main(): startTime = datetime.now() testbed = create_device_inventory() #from dynamic_testbed file #pprint.pprint(testbed) tb = load(testbed) client = GenieClient(tb, log=False) #saves the devices configuration devices = client.devices.values() device_config_dir = create_config_dir() pcall_task = pcall( save_config_to_file, device=devices, ) total_time = (datetime.now() - startTime) print('Script took {} to complete'.format(total_time))
def clean(self, device, reporter, *args, **kwargs): # In this section we will convert to Genie Testbed testbed = load(device.testbed) device = testbed.devices[device.name] global_stage_reuse_limit = getattr(self, 'global_stage_reuse_limit', GLOBAL_STAGE_REUSE_LIMIT) clean_testcase = CleanTestcase(device, global_stage_reuse_limit) clean_testcase.reporter = reporter.testcase(clean_testcase) with clean_testcase: # 1. Figure out what section to run # 2. Run them result = clean_testcase() if not result: raise Exception("Clean {result}.".format(result=str(result)))