Example #1
0
def test_print_structured_result_non_scrapli(capsys):
    test_agg_result_fail_to_string = TEST_AGG_RESULT
    del test_agg_result_fail_to_string["sea-ios-1"][0].scrapli_response
    print_structured_result(TEST_AGG_RESULT, fail_to_string=True)
    captured = capsys.readouterr()
    assert (
        captured.out ==
        """\x1b[1m\x1b[36msend_commands*******************************************************************\n\x1b[1m\x1b[34m* sea-ios-1 ** changed : False *************************************************\n\x1b[1m\x1b[32mvvvv send_commands ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO\nCisco IOS XE Software, Version 16.04.01\nCisco IOS Software [Everest], CSR1000V Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 16.4.1, RELEASE SOFTWARE (fc2)\nTechnical Support: http://www.cisco.com/techsupport\nCopyright (c) 1986-2016 by Cisco Systems, Inc.\nCompiled Sun 27-Nov-16 13:02 by mcpre\n\n\nCisco IOS-XE software, Copyright (c) 2005-2016 by cisco Systems, Inc.\nAll rights reserved.  Certain components of Cisco IOS-XE software are\nlicensed under the GNU General Public License ("GPL") Version 2.0.  The\nsoftware code licensed under GPL Version 2.0 is free software that comes\nwith ABSOLUTELY NO WARRANTY.  You can redistribute and/or modify such\nGPL code under the terms of GPL Version 2.0.  For more details, see the\ndocumentation or "License Notice" file accompanying the IOS-XE software,\nor the applicable URL provided on the flyer accompanying the IOS-XE\nsoftware.\n\n\nROM: IOS-XE ROMMON\n\ncsr1000v uptime is 2 hours, 43 minutes\nUptime for this control processor is 2 hours, 45 minutes\nSystem returned to ROM by reload\nSystem image file is "bootflash:packages.conf"\nLast reload reason: reload\n\n\n\nThis product contains cryptographic features and is subject to United\nStates and local country laws governing import, export, transfer and\nuse. Delivery of Cisco cryptographic products does not imply\nthird-party authority to import, export, distribute or use encryption.\nImporters, exporters, distributors and users are responsible for\ncompliance with U.S. and local country laws. By using this product you\nagree to comply with applicable laws and regulations. If you are unable\nto comply with U.S. and local laws, return this product immediately.\n\nA summary of U.S. laws governing Cisco cryptographic products may be found at:\nhttp://www.cisco.com/wwl/export/crypto/tool/stqrg.html\n\nIf you require further assistance please contact us by sending email to\[email protected].\n\nLicense Level: ax\nLicense Type: Default. No valid license found.\nNext reload license Level: ax\n\ncisco CSR1000V (VXE) processor (revision VXE) with 2052375K/3075K bytes of memory.\nProcessor board ID 9FKLJWM5EB0\n10 Gigabit Ethernet interfaces\n32768K bytes of non-volatile configuration memory.\n3985132K bytes of physical memory.\n7774207K bytes of virtual hard disk at bootflash:.\n0K bytes of  at webui:.\n\nConfiguration register is 0x2102\n\ncsr1000v#\nCodes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP\n       D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area\n       N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2\n       E1 - OSPF external type 1, E2 - OSPF external type 2\n       i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2\n       ia - IS-IS inter area, * - candidate default, U - per-user static route\n       o - ODR, P - periodic downloaded static route, H - NHRP, l - LISP\n       a - application route\n       + - replicated route, % - next hop override, p - overrides from PfR\n\nGateway of last resort is not set\n\n      10.0.0.0/8 is variably subnetted, 2 subnets, 2 masks\nC        10.0.0.0/24 is directly connected, GigabitEthernet1\nL        10.0.0.15/32 is directly connected, GigabitEthernet1\n\x1b[1m\x1b[32m^^^^ END send_commands ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n"""
    )
Example #2
0
def test_print_structured_result_fallback(capsys):
    test_agg_result_fail_to_string = TEST_AGG_RESULT
    test_agg_result_fail_to_string["sea-ios-1"][0].scrapli_response[
        0].textfsm_platform = "nope"
    print_structured_result(test_agg_result_fail_to_string,
                            fail_to_string=True)
    captured = capsys.readouterr()
    assert (
        captured.out ==
        """\x1b[1m\x1b[36msend_commands*******************************************************************\n\x1b[1m\x1b[34m* sea-ios-1 ** changed : False *************************************************\n\x1b[1m\x1b[32mvvvv send_commands ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO\nCisco IOS XE Software, Version 16.04.01\nCisco IOS Software [Everest], CSR1000V Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 16.4.1, RELEASE SOFTWARE (fc2)\nTechnical Support: http://www.cisco.com/techsupport\nCopyright (c) 1986-2016 by Cisco Systems, Inc.\nCompiled Sun 27-Nov-16 13:02 by mcpre\n\n\nCisco IOS-XE software, Copyright (c) 2005-2016 by cisco Systems, Inc.\nAll rights reserved.  Certain components of Cisco IOS-XE software are\nlicensed under the GNU General Public License ("GPL") Version 2.0.  The\nsoftware code licensed under GPL Version 2.0 is free software that comes\nwith ABSOLUTELY NO WARRANTY.  You can redistribute and/or modify such\nGPL code under the terms of GPL Version 2.0.  For more details, see the\ndocumentation or "License Notice" file accompanying the IOS-XE software,\nor the applicable URL provided on the flyer accompanying the IOS-XE\nsoftware.\n\n\nROM: IOS-XE ROMMON\n\ncsr1000v uptime is 2 hours, 43 minutes\nUptime for this control processor is 2 hours, 45 minutes\nSystem returned to ROM by reload\nSystem image file is "bootflash:packages.conf"\nLast reload reason: reload\n\n\n\nThis product contains cryptographic features and is subject to United\nStates and local country laws governing import, export, transfer and\nuse. Delivery of Cisco cryptographic products does not imply\nthird-party authority to import, export, distribute or use encryption.\nImporters, exporters, distributors and users are responsible for\ncompliance with U.S. and local country laws. By using this product you\nagree to comply with applicable laws and regulations. If you are unable\nto comply with U.S. and local laws, return this product immediately.\n\nA summary of U.S. laws governing Cisco cryptographic products may be found at:\nhttp://www.cisco.com/wwl/export/crypto/tool/stqrg.html\n\nIf you require further assistance please contact us by sending email to\[email protected].\n\nLicense Level: ax\nLicense Type: Default. No valid license found.\nNext reload license Level: ax\n\ncisco CSR1000V (VXE) processor (revision VXE) with 2052375K/3075K bytes of memory.\nProcessor board ID 9FKLJWM5EB0\n10 Gigabit Ethernet interfaces\n32768K bytes of non-volatile configuration memory.\n3985132K bytes of physical memory.\n7774207K bytes of virtual hard disk at bootflash:.\n0K bytes of  at webui:.\n\nConfiguration register is 0x2102\n\ncsr1000v#\n\x1b[1m\x1b[32m---- send_commands ** changed : False ------------------------------------------ INFO\n[ { \'distance\': \'\',\n    \'mask\': \'24\',\n    \'metric\': \'\',\n    \'network\': \'10.0.0.0\',\n    \'nexthop_if\': \'GigabitEthernet1\',\n    \'nexthop_ip\': \'\',\n    \'protocol\': \'C\',\n    \'type\': \'\',\n    \'uptime\': \'\'},\n  { \'distance\': \'\',\n    \'mask\': \'32\',\n    \'metric\': \'\',\n    \'network\': \'10.0.0.15\',\n    \'nexthop_if\': \'GigabitEthernet1\',\n    \'nexthop_ip\': \'\',\n    \'protocol\': \'L\',\n    \'type\': \'\',\n    \'uptime\': \'\'}]\n\x1b[1m\x1b[32m^^^^ END send_commands ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n"""
    )
Example #3
0
def run_get_config(command, structured, table, device, group):
    nr = InitNornir(config_file=f"{config_file}")
    if device:
        nr = nr.filter(name=f"{device}")
    if group:
        nr = nr.filter(F(groups__contains=f"{group}"))
    result = nr.run(task=get_config, command=command)
    if structured:
        print_structured_result(result, parser="genie")
    elif table:
        print_table_result(result, parser="genie")
    else:
        print_result(result)
Example #4
0
def main() -> None:
    nr = InitNornir(config_file="nornir_data/config2.yaml")
    spine = nr.filter(F(groups__contains="spine"))
    show_result = spine.run(task=send_command, command="show run | sec ospf")

    # we can print results w/ the "normal" print_result function, however with this method we have
    # no means to print the *structured* result (as parsed by textfsm/genie)
    print_result(show_result)

    # if we wanted to print this result data out parsed with genie we can do so as follows:
    print_structured_result(result=show_result, parser="genie")

    # or if we preferred textfsm - note that `textfsm` is the default parser argument so we don't
    # actually need to pass that here):
    print_structured_result(result=show_result, parser="textfsm")

    # if we had some kind of output that could *not* be parsed by the parser of our choice, the
    # printed output would be an empty list; if you would prefer to fallback to having the "raw"
    # string data printed if parsing fails, you can pass the `fail_to_string` argument:
    print_structured_result(result=show_result,
                            parser="textfsm",
                            fail_to_string=True)

    # if you prefer to fetch/store/manipulate the structured data instead of simply printing it, you
    # can do so by accessing the underlying scrapli `Response` object... first to keep this easy to
    # read we can snag the specific host results out of the nornir `AggregateResult` object:
    host_result = show_result["iosxe1"][0]

    # now we can see we've gotten the `Result` object -- or rather the `ScrapliResult` object from
    # the `AggregateResult`, and we can inspect that object to see what methods/attributes are
    # available to us
    print(type(host_result))
    print(dir(host_result))

    # in this case we can see there is a `scrapli_response` object which is the original/unmodified
    # response object from the underlying scrapli connection
    print(type(host_result.scrapli_response))
    print(dir(host_result.scrapli_response))

    # At this point we can simply work with that `scrapli_response` like we would a "normal" scrapli
    # response object:
    textfsm_results = host_result.scrapli_response.textfsm_parse_output()
    genie_results = host_result.scrapli_response.genie_parse_output()

    print("TEXTFSM RESULTS: \n", textfsm_results)
    print("GENIE RESULTS: \n", genie_results)
Example #5
0
def test_print_structured_result(capsys, to_dict):
    print_structured_result(TEST_AGG_RESULT, to_dict=to_dict[0])
    captured = capsys.readouterr()
    assert captured.out == to_dict[1]
Example #6
0
def test_print_structured_result_genie_to_dict(capsys, structured_result):
    print_structured_result(TEST_AGG_RESULT, parser="genie")
    captured = capsys.readouterr()
    assert captured.out == structured_result[1]