def tb_kickoff(testbed_name): import shutil files=os.listdir(tb_fl) #tftp_dir = "/auto/tftp-sjc-users2/rchaowdh/athena/logs/"+os.path.basename(destn) tftp_dir = destn if not os.path.exists(tftp_dir): os.makedirs(tftp_dir) print(files, tftp_dir) # qiangwa modi for file in files: if ".cfg" or ".virl" in fi: print(tb_fl, file, tftp_dir) shutil.copy(tb_fl+"/"+file, tftp_dir) # for file in files: # if ".cfg" in file: # print(tb_fl, file, tftp_dir) # shutil.copy(tb_fl+"/"+file, tftp_dir) # with pysftp.Connection(server_ip, username=user_name, password=passwrd) as sftp: # if not sftp.exists(destn): # sftp.mkdir(destn, mode=755) # with sftp.cd(destn): # for file in files: # if ".cfg" in file: # src=tb_fl+"/"+file # sftp.put(src) # if ".virl" in file: # src=tb_fl+"/"+file # sftp.put(src) # if ".ova" in file: # src=tb_fl+"/"+file # sftp.put(src) # os.remove(src) # if ".py" in file: # src=tb_fl+"/"+file # sftp.put(src) '''for image in imageDict: try: os.remove(imageDict[image]) except: print("CON")''' rply=OrderedDict() rp=r'uc_send "%s\r"' pr=r'\[sudo\] password for %s:' rply[pr %user_name]=rp %passwrd rply[r'This is your AD password:'******'This is your UNIX password:'******'cd %s'" %destn) testbed.devices[server_ip].execute("cd %s" %destn) print("Executing command 'vmcloud netdelete -t %s'" %testbed_name) op=testbed.devices[server_ip].execute("vmcloud netdelete -t %s" %testbed_name, timeout=600) print(op) time.sleep(30) print("Executing command 'vmcloud netcreate -v CSR1Kv.virl -t %s'" %testbed_name) op=testbed.devices[server_ip].execute("vmcloud netcreate -v CSR1Kv.virl -t %s" %testbed_name, timeout=600) #testbed.devices[server_ip].execute("cd ..") print(op)
def tb_kickoff(testbed_name): import shutil files = os.listdir(tb_fl) #tftp_dir = "/auto/tftp-sjc-users2/rchaowdh/athena/logs/"+os.path.basename(destn) tftp_dir = destn #qiangwa comment if not os.path.exists(tftp_dir): os.makedirs(tftp_dir) print(files, tftp_dir) # qiangwa modi for file in files: if ".cfg" or ".virl" in fi: print(tb_fl, file, tftp_dir) shutil.copy(tb_fl + "/" + file, tftp_dir) rply = OrderedDict() rp = r'uc_send "%s\r"' pr = r'\[sudo\] password for %s:' rply[pr % user_name] = rp % passwrd rply[r'This is your AD password:'******'This is your UNIX password:'******'cd %s'" % destn) testbed.devices[server_ip].execute("cd %s" % destn) print("Executing command 'vmcloud netdelete -t %s'" % testbed_name) op = testbed.devices[server_ip].execute("vmcloud netdelete -t %s" % testbed_name, timeout=180) print(op) time.sleep(30) print("Executing command 'vmcloud netcreate -v CSR1Kv.virl -t %s'" % testbed_name) op = testbed.devices[server_ip].execute( "vmcloud netcreate -v CSR1Kv.virl -t %s" % testbed_name, timeout=600) #testbed.devices[server_ip].execute("cd ..") print(op)
def create_custom_image(topo): global new_name, name_for_server, imageDict imageDir = "/home/cisco/image" #new_name=re.findall(r'.*/([A-Za-z0-9_.-]+)\.ova', csr_image)[0] tb_fl = dr + "/" + topo os.mkdir(tb_fl) rply = OrderedDict() rp = r'uc_send "%s\r"' pr = r'\[sudo\] password for %s:' rply[pr % user_name] = rp % passwrd rply[r'This is your AD password:'******'This is your UNIX password:'******'cd %s'" % imageDir) testbed.devices[server_ip].execute("cd %s" % imageDir) #create a set of images imageList = [] imageList.append(transitHubImage) imageList.append(branch1Image) imageList.append(branch2Image) imageList.append(hubImage) imageSet = set(imageList) print(imageSet) imageDict = {} image_name_list = [] for image in imageSet: new_name = re.findall(r'.*/([A-Za-z0-9_.-]+)\.ova', image)[0] name_for_server = new_name + "_10nic.ova" targetDST = imageDir + "/" + name_for_server if new_name not in image_name_list: image_name_list.append(new_name) new_name = targetDST k = [ 'cot', '-f', 'edit-hardware', 'aa', '-o', 'bb', '-c', '2', '-m', '4GB', '-n', '10', '--nic-types', 'virtio' ] for i in range(0, len(k)): if k[i] == "aa": k[i] = image if k[i] == "bb": k[i] = new_name cot_cmd = '' for cmd in k: cot_cmd = cot_cmd + cmd + ' ' print("Executing command '%s'" % cot_cmd) op = testbed.devices[server_ip].execute(cot_cmd) # print(op) if str(op) != "": print(banner("image %s with 10 NICS built" % image)) else: print( banner( "Error in building a 10 NIC image from source image. Exiting proc!!!!" )) if image == hubImage: imageDict['hub'] = targetDST if image == transitHubImage: imageDict['transit'] = targetDST if image == branch1Image: imageDict['branch1'] = targetDST if image == branch2Image: imageDict['branch2'] = targetDST
import os from genie.conf import Genie # import the topology module from ats.topology import loader # import the genie libs from genie.abstract import Lookup from genie.libs import ops # load testbed file which describes our devices pyats_testbed = loader.load('./default_testbed.yaml') # pyats testbed != genie testbed genie_testbed = Genie.init(pyats_testbed) print(genie_testbed.devices) all_interfaces = dict() for name, device in genie_testbed.devices.items(): print("Gathering Interface Information from {}".format(name)) device.connect() abstract = Lookup.from_device(device) intf = abstract.ops.interface.interface.Interface(device) intf.learn() all_interfaces[name] = intf.info for device, ints in all_interfaces.items(): for name, props in ints.items(): counters = props.get('counters') if counters: print('{}:{} CRC Errors: {}'.format(device, name, counters['in_crc_errors']))
#! python3 # checkConfig.py - get current config of device & compare to pre-stored config. # checkConfig.py usage: # python checkConfig.py <device_hostname> - get & compare config of a specific device, device must exist in my_testbed.yaml file # my_testbed.yaml file must exist in current working directory # pre-stored config must be collected & put to 'config' directory first ############################## Import neccessary libraries/modules ############################### import pickle import sys import pprint from ats.topology import loader from genie.conf import Genie #define global variables testbed = loader.load('my_testbed.yaml') genie_testbed = Genie.init(testbed) logfile = open('log_check.txt', 'w+') # python check_config.py <device_hostname> - get & compare config of a specific device, device must exist in my_testbed.yaml file if len(sys.argv) < 2: print('Please input the device name') print('Usage: python check_config.py <device_hostname>') sys.exit() dev = ' '.join(sys.argv[1:]) print('Get config of ' + str(dev) + '\n') logfile.write('Get config of ' + str(dev) + '\n') device = genie_testbed.devices[str(dev)] device.connect(learn_hostname=True) output_post = device.parse('show running-config') with open('config/running-config_' + str(device.name) + '.txt', 'rb') as f:
# my_testbed.yaml file must exist in current working directory # cmdRouter.csv file must exist in current working directory # Example of cmdRouter.csv: # command/rustphrbtqfb-1.txt,rustphrbtqfb-1 # command/rustphrbtqfb-2.txt,rustphrbtqfb-2 #Import neccessary libraries/modules import pickle import sys import pprint from ats.topology import loader from genie.conf import Genie import csv #Define global variables testbed = loader.load('my_testbed.yaml') #Load testbed yaml file genie_testbed = Genie.init(testbed) #Init genie testbed logfile = open('log.txt', 'w+') #Open log file #Define readCommandRouterFile() --- read csv file & return to data structure obj def readCommandRouterFile(cmdRouterFile): with open(cmdRouterFile) as f: csvf = csv.reader(f) data = list(csvf) return data #Define readCommandFile() --- read command txt file & return to cmd structure obj def readCommandFile(cmdFile): with open(cmdFile) as f: cmd = f.read().splitlines() return cmd
#! python3 # netlib_check.py - get current config of device, check & troubleshoot the state. # Usage: # python netlib_check.py <device_hostname> - device must exist in my_testbed.yaml file # my_testbed.yaml file must exist in current working directory ############################## Import neccessary libraries/modules ############################### import pickle import sys import pprint from ats.topology import loader from genie.conf import Genie ############################## Define global variables ############################### testbed = loader.load( '/root/workspace/devnet-2595/workshop/genie/my_testbed.yaml' ) #location of testbed file genie_testbed = Genie.init(testbed) #load testbed file to genie logfile = open('log_netlib_check.txt', 'w+') #open logfie ###Check input argument if len(sys.argv) < 2: #if there is NO hostname arg print('Please input the device name') print('Usage: python netlib_check.py <device_hostname>') sys.exit() dev = ' '.join(sys.argv[1:]) #if there is hostname, put it in 'dev' variable ############################## Define functions ############################### def check_bgp_status(): print('Get BGP status of ' + str(dev) + '\n') logfile.write('Get BGP status of ' + str(dev) + '\n')
def main(): parser = argparse.ArgumentParser(description="standalone parser") parser.add_argument('--testbed', dest='testbed', type=loader.load) args, unknown = parser.parse_known_args() if not args.testbed: print(""" ************************************************************** You didn't specify a testbed, we'll load up a Devnet sandbox. Next time try with your own testbed file by running ipyats --testbed <your file> ************************************************************** """) path = os.path.abspath(os.path.join(ipyats.__file__, os.pardir)) testbed = path + '/public_testbeds/devnet_sandbox.yaml' testbed = loader.load(testbed) testbed = Genie.init(testbed) else: # pyats testbed != genie testbed testbed = Genie.init(args.testbed) print(""" Welcome to ipyATS! your testbed is now available as the `testbed` object You can start by exploring some of the common operations available for the testbed by typing dir(testbed) To get you started, we've also included some common tasks, give them a try and then checkout the code in the `tasks` folder dir(tasks) You can checkout the source code for any of these funtions by passing them to show_source e.g show_source(tasks.get_routing_table) """) # if it's sandbox, these will come in handy if testbed.name == "devnet_always_on_sandbox": csr = testbed.devices['csr1000v'] # noqa nx = testbed.devices['sbx-n9kv-ao'] # noqa print(""" Detected Always-On Sandbox, creating some handy objects Created objects `csr` and `nx`") csr = testbed.devices['csr1000v'] nx = testbed.devices['sbx-n9kv-ao'] """) # bail to iPython embed()
""" import sys from genie.conf import Genie # import the topology module from ats.topology import loader # you're gonna love Lookup from genie.abstract import Lookup # gotcha: if your linitng you code this may raise an error but is necessary # it's not used until we perform a lookup from genie.libs import ops # noqa # load testbed file which describes our devices pyats_testbed = loader.load('../test_testbed.yml') # pyats testbed != genie testbed genie_testbed = Genie.init(pyats_testbed) # pick a device from the testbed dist1 = genie_testbed.devices['dist1'] dist1.connect() # dist1 is now your connection to the device # what do you want to do? # dist1.execute('show version') # dist1.configure('ip domain-name netdevlops.com') # dist1.ping('192.168.0.1') # but if you were interested in running CLI, you wouldnt be using pyATS,
class Common_setup(aetest.CommonSetup): """Common Setup section.""" # IMPORT THE JOBFILE INTO NAMESPACE jobfile = runtime.job.name # GET JOB FILE NAME a = importlib.import_module(jobfile) # IMPORT THE JOB FILE INTO NAMESPACE ScriptArgs = a.ScriptArgs() # INSTANCE OF SRSCRIPTARGS # OPTINAL ARGS FROM JOB FILE try: get_score = ScriptArgs.get_score except: get_score = None log.info('Setting get_score to {}'.format(get_score)) log.info(banner('get_score has been set to {}'.format(get_score))) # IMPORT YAML FILE ATTRIBUTES testbed = loader.load(ScriptArgs.testbed_file) rtr = testbed.devices[ScriptArgs.rtr] mgmt_ip = rtr.connections.vty_1['ip'] user_tftp_dir = ScriptArgs.user_tftp_dir platform = ScriptArgs.platform image_repo = ScriptArgs.image_repo tftp_ip = ScriptArgs.tftp_ip user = rtr.tacacs.username passwd = rtr.passwords['line'] if platform == 'asr9k-px': os_type = 'cXR' else: os_type = 'eXR' log.info('OS TYPE IS {}'.format(os_type)) log.info(banner('@@@ ATTEMPTING TO TELNET TO ROUTER {} @@@'.format(rtr))) # CONNECTING TO ROUTER VIA VTY LINE rtr.connect(via='vty_1', alias='mgmt1') rtr.connect(via='vty_2', alias='mgmt2') # CHECK PLATFORM TYPE platform = rtr.mgmt1.adminexec('show inventory chassis') m = re.search(r'Descr: +(NCS\d+|ASR-\d+|"ASR \d+|ASR +\d+|"ASR-\d+)', platform, re.IGNORECASE) try: log.info('Matthew m.group is {}'.format(m.group(0))) platform = m.group(0) except: log.info('COULD NOT MATCH PLATFORM TYPE IN REGEX') if 'NCS' in platform or 'ncs' in platform: log.info('ncs platform type {}'.format(platform)) platform_type = 'ncs' else: log.info('asr9k platform type {}'.format(platform)) platform_type = 'asr9k' global os_type_list os_type_list = [] os_type_list.append(os_type) # GET ALL THE LINECARDS THAT ARE UP IN TESTBED testbed_lc = get_all_linecards(os_type, rtr) lc_result = poll_lc_is_up(testbed_lc, os_type, rtr) if lc_result != 0: self.failed('LC did not return to up state.') log.info(banner('get_score has been set to {}'.format(get_score))) if get_score == True: # DATABASE ARGUMENTS db_host = ScriptArgs.db_host db_name = ScriptArgs.db_name db_collection = ScriptArgs.db_collection log.info('ATTEMPTING TO CONNECT TO IXIA VIA REST API') ixia = IxNetRestMain(ScriptArgs.ixia_chassis_ip, '11009') sessionId = ixia.sessionId log.info('sessionId = {}'.format(sessionId)) # CHECK IF IXIA CONNECTED try: if re.match('http:\/\/\d+.\d+.\d+.\d+:11009', sessionId): log.info('IXIA REST API connected via {}'.format(sessionId)) except: log.info('Ixia not connected') log.info('STARTING IXIA TRAFFIC...PLEASE WAIT') try: ixia.startTraffic() except: log.info('ERROR STARTING IXIA TRAFFIC') time.sleep(180) # CALLING RTR LOGGING FUNCTION os_type, chassis, cur_image, db_insert_data_dict = \ meritAPI.Report.rtr_log(rtr, traffic_state='pre_traffic') # SETTING RELEASE VERSION release = cur_image.group(1) # CALLING PRE TRAFFIC CHECK METHOD pre_traffic_stream_data = \ meritAPI.Traffic_data.traffic_check(ixia, traffic_state='pre_traffic') # SEND DATA TO LOG update_data_dict = meritAPI.Report.traffic_log( traffic_state='pre_traffic', traffic_dict=pre_traffic_stream_data) pre_upgrade_config = rtr.mgmt1.execute('show run') db_insert_data_dict.update(update_data_dict) # REMOVING INACTIVE PIES remove_inactive_pies(rtr, os_type)
from ats.topology import loader from genie.conf import Genie from genie.libs.conf.interface.iosxr import Interface import pprint from genie.libs.parser.iosxr.show_interface import ShowIpInterfaceBrief # Load setup details pyats_testbed = loader.load('testbed.yaml') testbed = Genie.init(pyats_testbed) # Connect to a device device = testbed.devices['xrv9k'] device.connect() # Parse operational output output = ShowIpInterfaceBrief(device=device) parsed_output = output.parse() # Print the output (optional) pprint.pprint(parsed_output) # Configure an interface intf1 = Interface(name='HundredGigE0/0/1/0', device=device) intf1.description = 'test' intf1.ipv4 = '203.0.113.11/24' intf1.ipv6 = '2001:db8:ff::11/64' cfgs = intf1.build_config() # See the changes device.execute('show run int hu0/0/1/0')
class Common_setup(aetest.CommonSetup): """Common Setup section.""" # IMPORT THE JOBFILE INTO NAMESPACE jobfile = runtime.job.name # GET JOB FILE NAME a = importlib.import_module(jobfile) # IMPORT THE JOB FILE INTO NAMESPACE ScriptArgs = a.ScriptArgs() # INSTANCE OF SRSCRIPTARGS # OPTINAL ARGS FROM JOB FILE try: load_image = ScriptArgs.load_image except: load_image = None try: traffic_compare = ScriptArgs.traffic_compare except: traffic_compare = None global no_image_found no_image_found = None try: image_available_date = ScriptArgs.image_available_date except: image_available_date = None # IMPORT YAML FILE ATTRIBUTES testbed = loader.load(ScriptArgs.testbed_file) rtr = testbed.devices[ScriptArgs.rtr] mgmt_ip = rtr.connections.vty_1['ip'] ixia = testbed.devices.ixia tcl_server = testbed.devices.ixia.connections.a.tcl_server user_tftp_dir = ScriptArgs.user_tftp_dir # IMAGE PATHS PASSED FROM JOB FILE base_path = ScriptArgs.base_image_repo user_tftp_dir = ScriptArgs.user_tftp_dir tftp_ip = ScriptArgs.tftp_ip # DATABASE ARGUMENTS db_host = ScriptArgs.db_host db_name = ScriptArgs.db_name db_collection = ScriptArgs.db_collection user = rtr.tacacs.username passwd = rtr.passwords['line'] log.info(banner('@@@ ATTEMPTING TO TELNET TO ROUTER {} @@@'.format(rtr))) # CONNECTING TO ROUTER VIA VTY LINE rtr.connect(via ='vty_1', alias = 'mgmt1') rtr.connect(via ='vty_2', alias = 'mgmt2') pre_upgrade_config = rtr.mgmt1.execute('show run') ixia = Ixia() ixia.connect(ixnetwork_tcl_server=tcl_server) ixia.traffic_control(action='run') log.info('Sleeping 3 min to allow traffic to start ' 'if it has not already been started') time.sleep(180) # CALLING RTR LOGGING FUNCTION os_type, chassis, cur_image, db_insert_data_dict = \ meritAPI.Report.rtr_log(rtr, traffic_state='pre_traffic') # SETTING RELEASE VERSION release = cur_image.group(1) # CALLING PRE TRAFFIC CHECK METHOD pre_traffic_stream_data = \ meritAPI.Traffic_data.traffic_check(ixia, traffic_state='pre_traffic') # SEND DATA TO LOG update_data_dict = meritAPI.Report.traffic_log(traffic_state='pre_traffic', traffic_dict=pre_traffic_stream_data) db_insert_data_dict.update(update_data_dict) # CHECK PLATFORM TYPE platform = rtr.mgmt1.adminexec('show inventory chassis') m = re.search(r'Descr: +(NCS\d+|ASR-\d+|"ASR \d+)', platform, re.IGNORECASE) platform = m.group(0) if 'NCS' in platform or 'ncs' in platform: log.info('ncs platform type {}'.format(platform)) platform_type = 'ncs' else: log.info('asr9k platform type {}'.format(platform)) platform_type = 'asr9k' global os_type_list os_type_list = [] os_type_list.append(os_type) # GET ALL THE LINECARDS THAT ARE UP IN TESTBED testbed_lc = get_all_linecards(os_type, rtr) lc_result = poll_lc_is_up(testbed_lc, os_type, rtr) if lc_result != 0: self.failed('LC did not return to up state.') # REMOVING INACTIVE PIES remove_inactive_pies(rtr, os_type)
from ats.topology import loader from ats import topology from ats.utils.fileutils import FileUtils # Transferring a single file to or from a remote server tb = loader.load("testbed.yaml") # Instanciate a filetransferutils instance for the device corresponding # to the device specific OS this_device = FileUtils.from_device(tb.devices['my_device']) # copy from remote to local machine this_device.copyfile(source='scp://remote_server:/tmp/demo.txt', destination='/Users/vkozin/Downloads/', timeout_seconds=15) # copy from local to remote machine this_device.copyfile(source='/Users/vkozin/Downloads/Task_1.docx', destination='scp://remote_server:/tmp/', timeout_seconds=15) # loading testbed immediately tb = topology.loader.load(''' devices: remote_device: os: 'linux' tacacs: username: vkozin passwords: linux: '159753852'
bot_email = os.getenv("TEAMS_BOT_EMAIL") teams_token = os.getenv("TEAMS_BOT_TOKEN") bot_url = os.getenv("TEAMS_BOT_URL") bot_app_name = os.getenv("TEAMS_BOT_APP_NAME") testbed_file = os.getenv("TESTBED_FILE") device_name = os.getenv("TESTBED_DEVICE") # Managed Device Details - Used for netmiko connection device_address = os.getenv("DEVICE_ADDRESS") device_type = os.getenv("DEVICE_TYPE") device_username = os.getenv("DEVICE_USERNAME") device_password = os.getenv("DEVICE_PASSWORD") # Setup pyATS and Genie device testbed = testbed_file testbed = loader.load(testbed) testbed = Genie.init(testbed) device = testbed.devices[device_name] # Create a Bot Object # Note: debug mode prints out more details about processing to terminal bot = TeamsBot( bot_app_name, teams_bot_token=teams_token, teams_bot_url=bot_url, teams_bot_email=bot_email, debug=True, ) # Create a function to respond to messages that lack any specific command
"""Example of pyATS work. Used just AETest package and data in the form""" import logging from ats import aetest from ats.topology import loader log = logging.getLogger(__name__) log.info('Loading testbed from YAML file') testbed = loader.load("testbed_1.yaml") class TestcaseOne(aetest.Testcase): server = None @aetest.setup def setup(self): log.info('Connection to Linux Server') self.server = testbed.devices['vm-a'] self.server.connect() @aetest.test def test_one(self): log.info('Check the results') virtual_machine_name = self.server.execute('hostname') assert len(virtual_machine_name) > 3 @aetest.cleanup def cleanup(self): log.info('Close connection to Server')
def copy_file(testbed, device, filename): tb = testbed d = device tftp_server = tb.servers['tftp'] tftp_address = tftp_server.address tftp_username = tftp_server.username tftp_password = tftp_server.password copy_cmd = 'copy ' + 'scp://' + tftp_address + filename + ' bootflash: vrf management' d.transmit(copy_cmd + '\r') if d.receive( r'Warning: There is already a file existing with this name. Do you want to overwrite' ): d.transmit('y\r') d.receive('Enter username: '******'\r') if d.receive(r'The authenticity of host '): d.transmit('yes\r') d.receive(r'password: '******'\r') time.sleep(20) if __name__ == '__main__': tb = loader.load('auto_brk_anchinch.yaml') device_name = 'bgl-n7k' d = tb.devices[device_name] d.connect() cmd = '''vlan 2121 interface port-channel 2121''' nxos_xmlin(d, 'JARVIS-2', cmd, 'edit-config')
from ats.topology import loader tb = loader.load("tb.yaml") dev = tb.devices['mydev'] dev.connect() dev.execute("hostname") dev.disconnect()