def check_compliance(hostname, test_file): opts = salt.config.master_config('/etc/salt/master') runner = salt.runner.RunnerClient(opts) pillar = runner.cmd('pillar.show_pillar', [hostname]) config_file_object = { "hosts": [ { "device": pillar['proxy']['host'], "username": pillar['proxy']['username'], "passwd": pillar['proxy']['passwd'] } ], "tests": [ test_file ] } config_file = yaml.dump(config_file_object) js = SnapAdmin() snapcheck_output = js.snapcheck(config_file, "automatic_snapshot_from_saltstack") json_output = {} for item in snapcheck_output: json_output = { 'jsnapy_device_name': hostname, 'jsnapy_device_ip': item.device, 'jsnapy_result': item.result, 'jsnapy_nbr_passed': item.no_passed, 'jsnapy_nbr_failed': item.no_failed, 'jsnapy_test_file': test_file } if json_output['jsnapy_result'] == 'Failed': caller = salt.client.LocalClient() caller.cmd(hostname, 'event.send', ['jnpr/compliance/failed'], kwarg={'result': json_output}) return json_output
def audit_config(*args, **kvargs): CONF_DATA = """ tests: - test_telnet.yml mail: /etc/jsnapy/send_mail.yml hosts: """ vMX_1_DATA = """ - device: 10.254.0.41 username: lab passwd: lab123 """ vMX_2_DATA = """ - device: 10.254.0.42 username: lab passwd: lab123 """ if "device" in kvargs: if kvargs["device"] == "vMX-1": CONF_DATA += vMX_1_DATA if kvargs["device"] == "vMX-2": CONF_DATA += vMX_2_DATA else: # possibly called by salt-run CONF_DATA += (vMX_1_DATA + vMX_2_DATA) snapadmin = SnapAdmin() result = snapadmin.snapcheck(CONF_DATA, "pre")
def __init__(self, hostname, model, ipaddress, username, password): self.hostname = hostname self.model = model self.username = username self.password = password self.ipaddress = ipaddress self.device = Device(host=ipaddress, user=username, password=password) self.snap = SnapAdmin()
def post_check(self): """ Run post-change snapshot and compare with pre """ js = SnapAdmin() try: self.post_snap = js.snap(self.template, "post") except Exception as ex: self.error = str(ex.args) + "\n" + str(ex.message) return self.post_compare()
def pre_check(self): """ Run pre-change snapshot """ js = SnapAdmin() try: self.pre_snap = js.snap(self.template, "pre") self.data = 'Pre-check snapshot complete' except Exception as ex: self.error = str(ex.args) + "\n" + str(ex.message) return
def jsnapy_post(task): host_check_yml = output_dir + task.host.name + '_check.yml' with open(host_check_yml, 'r') as f: host_check_resutl = f.read() js = SnapAdmin() config_host = config_data.format(task.host.hostname, task.host.username, task.host.password, host_check_resutl) js.snap(config_host, "post") snapchk = js.check(config_host, "pre", "post") result_output = '{}{}_{}_result.txt' for val in snapchk: with open(result_output.format(output_dir, task.host.name, dates), 'w') as f: f.write(json.dumps(dict(val.test_details), indent=4))
def config_verification(services, args): """Perform config verification with JSNAPy """ def get_test_text(name, port): return """ --- test_app_%s: - rpc: get-config - item: id: ./name xpath: 'applications/application[name="k8s%s"]' tests: - is-equal: destination-port, %s info: "Test Succeeded!!, destination-port is <{{post['destination-port']}}>" err: "Test Failed!!!, destination-port is <{{post['destination-port']}}>" """ % (name, name, port) jsnapy_config = { "hosts": [{ "device": "127.0.0.1", "username": "******", "passwd": "Juniper", "port": args.vsrx_port }], "tests": [] } for service in services: # Create test file for this service test_filename = "scripts/jsnapytest_%s.yaml" % service["name"] with open(test_filename, 'w') as testfile: testfile.write(get_test_text(service["name"], service["port"])) # Add reference for this test file to config jsnapy_config["tests"].append(test_filename) # Write config file to disk with open('scripts/jsnapyconfig.yaml', 'w') as configfile: yaml.dump(jsnapy_config, configfile, default_flow_style=False) # Retrieve instant snapshot and run tests on result js = SnapAdmin() chk = js.snapcheck('scripts/jsnapyconfig.yaml') # Ensure all tests passed for check in chk: assert check.result == "Passed"
def test_snap_2(self, mock_log, mock_etree, mock_dev, mock_parser, mock_exit, mock_path): js = SnapAdmin() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main.yml') config_file = open(conf_file, 'r') js.main_file = yaml.load(config_file, Loader=yaml.FullLoader) dev = jnpr.junos.device.Device(host="1.1.1.1", user="******", passwd="xyz") dev.open() m_op = mock_open() with (patch('jnpr.jsnapy.snap.open', m_op, create=True)) as (m_open): mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') js.generate_rpc_reply(dev, self.output_file, "1.1.1.1", js.main_file) self.assertTrue(mock_path.called) dev.close()
def jsnapy_pre(task, jsnapy_test): if jsnapy_test: task.host['host_checks'] = jsnapy_test else: task.run(task=gather_data) task.run(task=get_host_check) host_check_result = '' for check in task.host['host_checks']: host_check_result += test_yml.format(template_path.format(check)) host_check_yml = output_dir + task.host.name + '_check.yml' with open(host_check_yml, 'w') as f: f.write(host_check_result) js = SnapAdmin() config_host = config_data.format(task.host.hostname, task.host.username, task.host.password, host_check_result) js.snap(config_host, "pre")
def post_compare(self): """ Run post check and gather output """ # Setup a memory buffer to capture logging messages from jsnapy jsnapy_log = logging.handlers.MemoryHandler(1024 * 10, logging.DEBUG) jsnapy_log.setLevel(logging.DEBUG) log = logging.getLogger() log.setLevel(logging.DEBUG) debug_format = logging.Formatter("%(message)") jsnapy_log.setFormatter(debug_format) log.addHandler(jsnapy_log) # Run jsnapy check on pre and post snapshots js = SnapAdmin() self.result = js.check(self.template, pre_file="pre", post_file="post") # Gather output from buffer post_log = [] for line in jsnapy_log.buffer: post_log.append(str(line.getMessage())) self.data = format_html.formatting(post_log)
def test_snap_2(self, mock_log, mock_etree, mock_dev, mock_parser, mock_exit, mock_path): js = SnapAdmin() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main.yml') config_file = open(conf_file, 'r') js.main_file = yaml.load(config_file) dev = jnpr.junos.device.Device( host="1.1.1.1", user="******", passwd="xyz") dev.open() m_op = mock_open() with (patch('jnpr.jsnapy.snap.open', m_op, create=True))as (m_open): mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') js.generate_rpc_reply( dev, self.output_file, "1.1.1.1", js.main_file) self.assertTrue(mock_path.called) dev.close()
def main(): js = SnapAdmin() config_file = "infra.yaml" js.snap(config_file, "module_snap1") js.snap(config_file, "module_snap1") chk = js.check(config_file, "module_snap1", "module_snap1") for check in chk: print "Tested on", check.device print "Final result: ", check.result print "Total passed: ", check.no_passed print "Total failed:", check.no_failed #pprint(dict(check.test_details)) if (check.result == "Failed"): print("The snapshot verification has failed") else: print("The snapshot verification was successful") pprint(dict(check))
### performing function similar to --snapcheck option in command line ###### from jnpr.jsnapy import SnapAdmin from pprint import pprint from jnpr.junos import Device js = SnapAdmin() config_file = "/etc/jsnapy/testfiles/config_single_snapcheck.yml" snapvalue = js.snapcheck(config_file, "snap") for snapcheck in snapvalue: print "\n -----------snapcheck----------" print "Tested on", snapcheck.device print "Final result: ", snapcheck.result print "Total passed: ", snapcheck.no_passed print "Total failed:", snapcheck.no_failed pprint(dict(snapcheck.test_details))
def invokeTests(vmanme): js = SnapAdmin() helpers = Helpers() config_file = """ hosts: - device: 192.168.122.9 username: ubuntu tests: - test_diff.yml """ #js.snap(config_file, "pre") js.snap(config_file, "post") chk = js.check(config_file, "pre", "post") ''' file_name = "/home/ubuntu/Evolved_Campus_Core_Automation/Inventory" with open(file_name) as f: content = f.read() campuses = re.findall('\[(.*?)\]',content) campuses_info = {} for campus in campuses: if "children" in campus: campus_id = campus.rsplit(":",1)[0] campuses_info.update({campus_id: {'leaf': [],'spine':[]}}) for campus in campuses: leaf_count = 0 spine_count = 0 campus_id = campus.rsplit("-")[1] if not "children"in campus: if "leaf" in campus: data_loader = DataLoader() inventory = InventoryManager(loader = data_loader, sources=[file_name]) lst = inventory.get_groups_dict()[campus] for ls in lst: campuses_info['campus-' + campus_id]['leaf'].append(ls) elif "spine" in campus: data_loader = DataLoader() inventory = InventoryManager(loader = data_loader, sources=[file_name]) lst = inventory.get_groups_dict()[campus] for ls in lst: campuses_info['campus-' + campus_id]['spine'].append(ls) ''' spine_leaf_info = {} ''' for campus in campuses_info: for leaf_dev in campuses_info[campus]["leaf"]: spine_leaf_info.update({leaf_dev: campuses_info[campus]["spine"]}) ''' devip = "" failed = 0 test_name = "" for check in chk: devip = check.device failed = check.no_failed if not not check.test_results.keys(): test_name = check.test_results.keys()[0].replace(' ','_') ae_id = "" dev = helpers.device_connect(devip) dev.open() data = dev.rpc.get_config(options={'format':'json'}) for ints in data['configuration']['interfaces']['interface']: if "ae" in ints['name']: ae_id = ints['name'] dev.close() pre_file = '/home/ubuntu/jsnapy/snapshots/' + devip + '_pre_' + test_name + '.xml' post_file = '/home/ubuntu/jsnapy/snapshots/' + devip + '_post_' + test_name + '.xml' dict = {} if failed != 0: with open(pre_file, 'r') as hosts0: with open(post_file, 'r') as hosts1: diff = difflib.unified_diff( hosts0.readlines(), hosts1.readlines(), fromfile='hosts0', tofile='hosts1', n=0, ) lines = list(diff)[2:] print lines added = [line[1:] for line in lines if line[0] == '+'] additions = "" for line in added: additions += line print 'additions' vlan_text = "" if additions: tree = xml.etree.ElementTree.fromstring(additions) print tree for vlan in tree.findall('l2ng-l2rtb-vlan-name'): text = vlan.text vlan_text = text.split("vlan")[1] print vlan_text dict = {'vqfx3': {'vlanid':vlan_text}} template_file = '/home/ubuntu/Evolved_Campus_Core_Automation/Config/QFX_vlan_leaf_addition.conf' helpers.load_template_config(devip,"spine",dict,template_file) ''' print spine_leaf_info for spine_ip in spine_leaf_info[devip]: template_file = '/home/ubuntu/Evolved_Campus_Core_Automation/Config/QFX_vlan_spine_addition.conf' #helpers.load_template_config(spine_ip,"spine",dict,template_file) ''' if failed != 0: with open(post_file) as f: lines = f.readlines() lines1 = "" for line in lines: lines1 += line with open(pre_file, "w") as f1: f1.writelines(lines1)
# usage of jsnapy python library. without pyez. from jnpr.jsnapy import SnapAdmin from pprint import pprint from slacker import Slacker # instanciate the class SnapAdmin js = SnapAdmin() # the variable config_file refers to the jsnapy configuration file config_file = "cfg_file_check_bgp_states.yml" # taking first snapshots using jsnapy # Performing function similar to --snap print "taking first snapshots using jsnapy" js.snap(config_file, "pre") # jsnapy closed the connection after the snapshot. # this is where you are supposed to apply your configuration changes # taking second snapshot using jsnapy # Performing function similar to --snap print "taking second snapshots using jsnapy" js.snap(config_file, "post") # jsnapy closed the connection after the snapshot. # Performing function similar to --check # comparing first and second snapshots using jsnapy. and printing the result. # sending slack notifications print "comparing first and second snapshots using jsnapy" chk = js.check(config_file, "pre", "post")
# performing function similar to --snapcheck option in command line # usage of jsnapy python library without pyez. from jnpr.jsnapy import SnapAdmin # instanciate the class SnapAdmin js = SnapAdmin() # the variable config_file refers to the jsnapy configuration file config_file = "cfg_file_snapcheck_bgp_states.yml" # Performing function similar to --snapcheck # taking a snapshot (called snap_from_python) and comparing it against predefined criteria snapvalue = js.snapcheck(config_file, "snap_from_python") # jsnapy closed the connection after the snapshot. # printing the result print "comparing snapshots against predefined criteria using jsnapy" for snapcheck in snapvalue: # print "\n -----------snapcheck----------" # print "Tested on", snapcheck.device # print "Final result: ", snapcheck.result # print "Total passed: ", snapcheck.no_passed # print "Total failed:", snapcheck.no_failed # pprint(dict(snapcheck.test_details)) if snapcheck.no_failed != 0: print "this device failed some tests: " + snapcheck.device
#! /Library/Frameworks/Python.framework/Versions/2.7/bin/python ### Example showing how to pass yaml data in same file ### from jnpr.jsnapy import SnapAdmin from pprint import pprint from jnpr.junos import Device js = SnapAdmin() config_data = """ hosts: - device: 172.16.226.10 username : silvia passwd: SilviaMurgescu tests: - test_exists.yml - test_contains.yml - test_is_equal.yml """ snapchk = js.snapcheck(config_data, "pre") for val in snapchk: print "Tested on", val.device print "Final result: ", val.result print "Total passed: ", val.no_passed print "Total failed:", val.no_failed pprint(dict(val.test_details))
# usage of jsnapy and pyez python libraries together. # Example showing how jsnapy can reuse an existing device connection from pyez # import jsnapy from jnpr.jsnapy import SnapAdmin # import pyez from jnpr.junos import Device from jnpr.junos.utils.config import Config from pprint import pprint # instanciate the class SnapAdmin js = SnapAdmin() # the variable config_file refers to the jsnapy configuration file config_file = "cfg_file_check_bgp_states.yml" # taking first snapshot using jsnapy # Performing function similar to --snap print "taking first snapshots using jsnapy ..." js.snap(config_file, "pre") # jsnapy closed the connection after the snapshot. # opening a new connection using pyez. my_devices_list=["172.30.179.74","172.30.179.73","172.30.179.95"] for item in my_devices_list: print "opening a connection to the junos device " + item + " using pyez ..." dev_obj = Device(host=item, user='******', password='******') dev_obj.open() # configuring the junos device using pyez
### Example showing how to pass yaml data in same file ### from jnpr.jsnapy import SnapAdmin from pprint import pprint from jnpr.junos import Device js = SnapAdmin() config_data = """ hosts: - device: 10.209.16.204 username : demo passwd: demo123 tests: - test_exists.yml - test_contains.yml - test_is_equal.yml """ snapchk = js.snapcheck(config_data, "pre") for val in snapchk: print "Tested on", val.device print "Final result: ", val.result print "Total passed: ", val.no_passed print "Total failed:", val.no_failed pprint(dict(val.test_details))
### Example showing how to use existing device connection ### from jnpr.jsnapy import SnapAdmin from pprint import pprint from jnpr.junos import Device dev_obj = Device(host='10.209.61.156', user='******', password='******') dev_obj.open() js = SnapAdmin() config_file = "/etc/jsnapy/testfiles/config_check.yml" # can pass device object # it will not create new connection with device snapchk = js.snapcheck(config_file, "snap", dev=dev_obj) for val in snapchk: print "Tested on", val.device print "Final result: ", val.result print "Total passed: ", val.no_passed print "Total failed:", val.no_failed print val.test_details pprint(dict(val.test_details))
from jnpr.jsnapy import SnapAdmin from pprint import pprint from jnpr.junos import Device from jnpr.junos.utils.config import Config config_data = """ tests: - test_file_snapcheck_bgp_states.yml """ js = SnapAdmin() my_devices_list = ["172.30.179.74", "172.30.179.73", "172.30.179.95"] for item in my_devices_list: print "opening a connection to a junos device using pyez" dev_obj = Device(host=item, user='******', password='******') dev_obj.open() cfg = Config(dev_obj) print "configuring the device " + dev_obj.facts["hostname"] cfg.load("set system login message hello", format='set') print "here's the configuring details pushed with pyez" cfg.pdiff() print "commiting the configuration change " if cfg.commit() == True: print "commit succeed" else: print "commit failed" print "auditing the device " + dev_obj.facts["hostname"] + " using jsnapy" snapchk = js.snapcheck(config_data, "snap", dev=dev_obj) print "rollback the device " + dev_obj.facts["hostname"] + " using pyez" cfg.rollback(rb_id=1)
# usage of jsnapy python library without pyez. # performing function similar to --snapcheck with --local in jsnapy command line tool from jnpr.jsnapy import SnapAdmin from pprint import pprint # instanciate the class SnapAdmin js = SnapAdmin() # the variable config_file refers to the jsnapy configuration file config_file = "cfg_file_snapcheck_bgp_states.yml" # taking a snapshot using jsnapy # Performing function similar to --snap print "taking snapshots using jsnapy" js.snap(config_file, "snapname") # jsnapy closed the connection after the snapshot. # Performing function similar to --snapcheck with --local # runs the tests on stored snapshots named snapname. # jsnapy doesnt open connection # To use this command one has to first create snapshot snapvalue = js.snapcheck(config_file, "snapname", local=True) # printing the result print "comparing snapshots against predefined criteria using jsnapy" for snapcheck in snapvalue: # print "\n -----------snapcheck----------" # print "Tested on", snapcheck.device # print "Final result: ", snapcheck.result # print "Total passed: ", snapcheck.no_passed
class Router: def __init__(self, hostname, model, ipaddress, username, password): self.hostname = hostname self.model = model self.username = username self.password = password self.ipaddress = ipaddress self.device = Device(host=ipaddress, user=username, password=password) self.snap = SnapAdmin() def open(self): self.device.open() self.device.bind(cu=Config) def lock(self): self.device.cu.lock() def unlock(self): self.device.cu.unlock() def close(self): self.device.close() def commit(self): return self.device.cu.commit() def rollback(self): return self.device.cu.rollback() def diff_config(self): if self.device.cu.diff(): message = self.device.cu.diff() else: message = '' return message def commit_check(self): return self.device.cu.commit_check() def load_config(self, operation_name, operation_param=None): set_result = False message = '' if operation_name == 'set_add_interface': template_filename = './set_templates/add_interface.jinja2' tamplate_param = operation_param elif operation_name == 'set_add_bgp_neighbor': template_filename = './set_templates/add_bgp_neighbor.jinja2' tamplate_param = operation_param elif operation_name == 'set_add_bgp_policy_external': template_filename =\ './set_templates/add_bgp_policy_external.jinja2' tamplate_param = operation_param else: pass config_txt = self.generate_from_jinja2(template_filename, tamplate_param) self.device.cu.load(template_path=template_filename, template_vars=tamplate_param, format="text", merge=True) message = config_txt set_result = True return set_result, message def nwtest(self, operation_name, operation_param=None): nwtest_result = False message = '' template_filename = '' if operation_name == 'nwtest_hostname': template_filename =\ './nwtest_templates/%s.jinja2' % (operation_name) tamplate_param = {'hostname': self.hostname} nwtest_filename =\ './nwtests/' +\ operation_name + '_' +\ self.hostname +\ '.yml' elif operation_name == 'nwtest_model': template_filename =\ './nwtest_templates/%s.jinja2' % (operation_name) tamplate_param = {'model': self.model} nwtest_filename =\ './nwtests/' +\ operation_name + '_' +\ self.hostname +\ '.yml' elif operation_name == 'nwtest_interface': template_filename =\ './nwtest_templates/%s.jinja2' % (operation_name) tamplate_param = operation_param nwtest_filename =\ './nwtests/' +\ operation_name + '_' +\ operation_param['interface_name'].replace('/', '-') + '_' +\ operation_param['interface_status'] +\ '.yml' elif operation_name == 'nwtest_bgp_neighbor': template_filename =\ './nwtest_templates/%s.jinja2' % (operation_name) tamplate_param = operation_param nwtest_filename =\ './nwtests/' +\ operation_name + '_' +\ operation_param['neighbor_address_ipv4'].replace('.', '-') + '_' +\ operation_param['neighbor_status'] +\ '.yml' elif operation_name == 'nwtest_bgp_received_route': template_filename =\ './nwtest_templates/%s.jinja2' % (operation_name) tamplate_param = operation_param nwtest_filename =\ './nwtests/' +\ operation_name + '_' +\ operation_param['neighbor_address_ipv4'].replace('.', '-') + '_' +\ operation_param['received_route_address_ipv4'].replace('.', '-') +\ '.yml' elif operation_name == 'nwtest_bgp_advertised_route': template_filename =\ './nwtest_templates/%s.jinja2' % (operation_name) tamplate_param = operation_param nwtest_filename =\ './nwtests/' +\ operation_name + '_' +\ operation_param['neighbor_address_ipv4'].replace('.', '-') + '_' +\ operation_param['advertised_route_address_ipv4'].replace('.', '-') +\ '.yml' elif operation_name == 'nwtest_ping': template_filename =\ './nwtest_templates/%s.jinja2' % (operation_name) tamplate_param = operation_param nwtest_filename =\ './nwtests/' +\ operation_name + '_' +\ operation_param['target_ipaddress'].replace('.', '-') +\ '.yml' else: pass self.generate_nwtestfile(template_filename=template_filename, template_param=tamplate_param, nwtest_filename=nwtest_filename) jsnapy_conf = 'nwtests:' + '\n' +\ ' - %s' % (nwtest_filename) snapcheck_dict = self.snap.snapcheck(data=jsnapy_conf, dev=self.device) for snapcheck in snapcheck_dict: if snapcheck.result == 'Passed': nwtest_result = True if operation_name == 'nwtest_bgp_received_route': expected_value = '%s/%s' % ( operation_param['received_route_address_ipv4'], operation_param['received_route_subnet_ipv4']) acutual_value =\ snapcheck.test_details.values()[0][0]['passed'][0]['pre'].keys()[0] elif operation_name == 'nwtest_bgp_advertised_route': expected_value = '%s/%s' % ( operation_param['advertised_route_address_ipv4'], operation_param['advertised_route_subnet_ipv4']) acutual_value =\ snapcheck.test_details.values()[0][0]['passed'][0]['pre'].keys()[0] else: expected_value =\ snapcheck.test_details.values()[0][0]['expected_node_value'] acutual_value =\ snapcheck.test_details.values()[0][0]['passed'][0]['actual_node_value'] message =\ 'nwtest file : %s\n' % nwtest_filename +\ 'expected value : %s\n' % (expected_value) +\ 'acutual value : %s' % (acutual_value) elif snapcheck.result == 'Failed': nwtest_result = False if operation_name == 'nwtest_bgp_received_route': expected_value = '%s/%s' % ( operation_param['received_route_address_ipv4'], operation_param['received_route_subnet_ipv4']) acutual_value = 'None' elif operation_name == 'nwtest_bgp_advertised_route': expected_value = '%s/%s' % ( operation_param['advertised_route_address_ipv4'], operation_param['advertised_route_subnet_ipv4']) acutual_value = 'None' else: expected_value =\ snapcheck.test_details.values()[0][0]['expected_node_value'] acutual_value =\ snapcheck.test_details.values()[0][0]['failed'][0]['actual_node_value'] message = 'nwtest file : %s\n' % nwtest_filename +\ 'expected value : %s\n' % (expected_value) +\ 'acutual value : %s' % (acutual_value) return nwtest_result, message def generate_nwtestfile(self, template_filename, template_param, nwtest_filename): nwtest_yml = self.generate_from_jinja2(template_filename, template_param) # write nwtest file (YAML format) with open(nwtest_filename, 'w') as f: f.write(nwtest_yml) def generate_from_jinja2(self, template_filename, template_param): # read template file (jinja2 format) with open(template_filename, 'r') as f: template_jinja2 = f.read() # generate nwtest file from template file return Environment().from_string(template_jinja2).render( template_param)
### Performing function similar to --check in command line ### from jnpr.jsnapy import SnapAdmin from pprint import pprint from jnpr.junos import Device js = SnapAdmin() config_file = "/etc/jsnapy/testfiles/config_check.yml" js.snap(config_file, "pre") js.snap(config_file, "post") chk = js.check(config_file, "pre", "post") for check in chk: print "Tested on", check.device print "Final result: ", check.result print "Total passed: ", check.no_passed print "Total failed:", check.no_failed print check.test_details pprint(dict(check.test_details))
import pprint from jnpr.jsnapy import SnapAdmin from jnpr.junos import Device CONFIG_FILE = 'test_show_default_route.yml' PRE_SNAP = 'pre_show_route_test.xml' POST_SNAP = 'post_show_route_test.xml' TESTS = {'tests': [CONFIG_FILE]} if __name__ == "__main__": device = Device(host='example.com', user='******', passwd='123456') snap = SnapAdmin() r = snap.check(TESTS, PRE_SNAP, POST_SNAP, device) pprint.pprint(r[0].test_results)
import time # In order to sleep and get time def myprint(str): print(time.strftime(" %H:%M:%S", time.gmtime()) + " " + str) host = raw_input('hostname: ') user = raw_input('username: '******'password: '******'Unable to connect: ', error exit() dev.timeout = 2500 js = SnapAdmin() config_file = "junos-upgrade.yml" myprint("Taking snapshot of states before the upgrade") snapvalue = js.snap(config_file, "pre", dev=dev) if snapvalue[0] == None: myprint( "*** Error while getting snapshot perhaps config file is not there") exit()
def myprint(str): print(time.strftime(" %H:%M:%S", time.gmtime()) + " " + str) host = raw_input('hostname: ') user = raw_input('username: '******'password: '******'Unable to connect: ', error exit() dev.timeout = 2500 js = SnapAdmin() config_file = "junos-upgrade.yml" myprint("waiting 10 seconds before taking post snapshot....") time.sleep(10) myprint("gathering snapshot after upgrade....") snapvalue = js.snap(config_file, "post", dev=dev) print myprint("Comparing pre and post snapshots") snapvalue = js.check(config_file, "pre", "post", dev=dev)