From 5f20365564f4be50297a2aec6e4b14414c56c29b Mon Sep 17 00:00:00 2001 From: xumia <59720581+xumia@users.noreply.github.com> Date: Sat, 29 May 2021 19:07:30 +0800 Subject: [PATCH] Change to use rvtysh when calling the show commands (#1572) Change to use rvtysh when calling the show commands --- show/bgp_common.py | 4 ++-- show/bgp_quagga_v4.py | 7 ++++--- show/bgp_quagga_v6.py | 7 ++++--- show/main.py | 13 +++++++------ tests/conftest.py | 9 +++++---- utilities_common/bgp_util.py | 12 +++++++----- utilities_common/constants.py | 3 ++- 7 files changed, 31 insertions(+), 24 deletions(-) diff --git a/show/bgp_common.py b/show/bgp_common.py index 50df00dcbbb8..6bf332859acc 100644 --- a/show/bgp_common.py +++ b/show/bgp_common.py @@ -377,9 +377,9 @@ def show_routes(args, namespace, display, verbose, ipver): # If not MultiASIC, skip namespace argument cmd = "show {} route {}".format(ipver, arg_strg) if multi_asic.is_multi_asic(): - output = bgp_util.run_bgp_command(cmd, ns) + output = bgp_util.run_bgp_show_command(cmd, ns) else: - output = bgp_util.run_bgp_command(cmd) + output = bgp_util.run_bgp_show_command(cmd) print("{}".format(output)) return diff --git a/show/bgp_quagga_v4.py b/show/bgp_quagga_v4.py index 3eb3f4bc12aa..e384cd9d17d0 100644 --- a/show/bgp_quagga_v4.py +++ b/show/bgp_quagga_v4.py @@ -1,6 +1,7 @@ import click from show.main import AliasedGroup, ip, run_command from utilities_common.bgp_util import get_bgp_summary_extended +import utilities_common.constants as constants ############################################################################### @@ -21,10 +22,10 @@ def bgp(): def summary(): """Show summarized information of IPv4 BGP state""" try: - device_output = run_command('sudo vtysh -c "show ip bgp summary"', return_cmd=True) + device_output = run_command('sudo {} -c "show ip bgp summary"'.format(constants.RVTYSH_COMMAND), return_cmd=True) get_bgp_summary_extended(device_output) except Exception: - run_command('sudo vtysh -c "show ip bgp summary"') + run_command('sudo {} -c "show ip bgp summary"'.format(constants.RVTYSH_COMMAND)) # 'neighbors' subcommand ("show ip bgp neighbors") @@ -34,7 +35,7 @@ def summary(): def neighbors(ipaddress, info_type): """Show IP (IPv4) BGP neighbors""" - command = 'sudo vtysh -c "show ip bgp neighbor' + command = 'sudo {} -c "show ip bgp neighbor'.format(constants.RVTYSH_COMMAND) if ipaddress is not None: command += ' {}'.format(ipaddress) diff --git a/show/bgp_quagga_v6.py b/show/bgp_quagga_v6.py index 9987588aa557..003f4c94cf58 100644 --- a/show/bgp_quagga_v6.py +++ b/show/bgp_quagga_v6.py @@ -1,6 +1,7 @@ import click from show.main import AliasedGroup, ipv6, run_command from utilities_common.bgp_util import get_bgp_summary_extended +import utilities_common.constants as constants ############################################################################### @@ -21,10 +22,10 @@ def bgp(): def summary(): """Show summarized information of IPv6 BGP state""" try: - device_output = run_command('sudo vtysh -c "show ipv6 bgp summary"', return_cmd=True) + device_output = run_command('sudo {} -c "show ipv6 bgp summary"'.format(constants.RVTYSH_COMMAND), return_cmd=True) get_bgp_summary_extended(device_output) except Exception: - run_command('sudo vtysh -c "show ipv6 bgp summary"') + run_command('sudo {} -c "show ipv6 bgp summary"'.format(constants.RVTYSH_COMMAND)) # 'neighbors' subcommand ("show ipv6 bgp neighbors") @@ -33,5 +34,5 @@ def summary(): @click.argument('info_type', type=click.Choice(['routes', 'advertised-routes', 'received-routes']), required=True) def neighbors(ipaddress, info_type): """Show IPv6 BGP neighbors""" - command = 'sudo vtysh -c "show ipv6 bgp neighbor {} {}"'.format(ipaddress, info_type) + command = 'sudo {} -c "show ipv6 bgp neighbor {} {}"'.format(constants.RVTYSH_COMMAND, ipaddress, info_type) run_command(command) diff --git a/show/main.py b/show/main.py index 1cea9e653484..3afca1c93a7b 100755 --- a/show/main.py +++ b/show/main.py @@ -13,6 +13,7 @@ from tabulate import tabulate from utilities_common import util_base from utilities_common.db import Db +import utilities_common.constants as constants from . import acl from . import bgp_common @@ -725,7 +726,7 @@ def mac(vlan, port, verbose): @click.option('--verbose', is_flag=True, help="Enable verbose output") def route_map(route_map_name, verbose): """show route-map""" - cmd = 'sudo vtysh -c "show route-map' + cmd = 'sudo {} -c "show route-map'.format(constants.RVTYSH_COMMAND) if route_map_name is not None: cmd += ' {}'.format(route_map_name) cmd += '"' @@ -782,7 +783,7 @@ def route(args, namespace, display, verbose): @click.option('--verbose', is_flag=True, help="Enable verbose output") def prefix_list(prefix_list_name, verbose): """show ip prefix-list""" - cmd = 'sudo vtysh -c "show ip prefix-list' + cmd = 'sudo {} -c "show ip prefix-list'.format(constants.RVTYSH_COMMAND) if prefix_list_name is not None: cmd += ' {}'.format(prefix_list_name) cmd += '"' @@ -794,7 +795,7 @@ def prefix_list(prefix_list_name, verbose): @click.option('--verbose', is_flag=True, help="Enable verbose output") def protocol(verbose): """Show IPv4 protocol information""" - cmd = 'sudo vtysh -c "show ip protocol"' + cmd = 'sudo {} -c "show ip protocol"'.format(constants.RVTYSH_COMMAND) run_command(cmd, display_cmd=verbose) @@ -817,7 +818,7 @@ def ipv6(): @click.option('--verbose', is_flag=True, help="Enable verbose output") def prefix_list(prefix_list_name, verbose): """show ip prefix-list""" - cmd = 'sudo vtysh -c "show ipv6 prefix-list' + cmd = 'sudo {} -c "show ipv6 prefix-list'.format(constants.RVTYSH_COMMAND) if prefix_list_name is not None: cmd += ' {}'.format(prefix_list_name) cmd += '"' @@ -865,7 +866,7 @@ def route(args, namespace, display, verbose): @click.option('--verbose', is_flag=True, help="Enable verbose output") def protocol(verbose): """Show IPv6 protocol information""" - cmd = 'sudo vtysh -c "show ipv6 protocol"' + cmd = 'sudo {} -c "show ipv6 protocol"'.format(constants.RVTYSH_COMMAND) run_command(cmd, display_cmd=verbose) # @@ -1092,7 +1093,7 @@ def ports(portname, verbose): @click.option('--verbose', is_flag=True, help="Enable verbose output") def bgp(verbose): """Show BGP running configuration""" - cmd = 'sudo vtysh -c "show running-config"' + cmd = 'sudo {} -c "show running-config"'.format(constants.RVTYSH_COMMAND) run_command(cmd, display_cmd=verbose) diff --git a/tests/conftest.py b/tests/conftest.py index 6ad352b224d8..c2e85fb90043 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -20,6 +20,7 @@ mock_show_bgp_network_multi_asic ) from . import config_int_ip_common +import utilities_common.constants as constants test_path = os.path.dirname(os.path.abspath(__file__)) modules_path = os.path.dirname(test_path) @@ -128,14 +129,14 @@ def setup_single_bgp_instance(request): bgp_mocked_json = os.path.join( test_path, 'mock_tables', 'dummy.json') - def mock_show_bgp_summary(vtysh_cmd, bgp_namespace): + def mock_show_bgp_summary(vtysh_cmd, bgp_namespace, vtysh_shell_cmd=constants.RVTYSH_COMMAND): if os.path.isfile(bgp_mocked_json): with open(bgp_mocked_json) as json_data: mock_frr_data = json_data.read() return mock_frr_data return "" - def mock_run_bgp_command_for_static(vtysh_cmd, bgp_namespace=""): + def mock_run_bgp_command_for_static(vtysh_cmd, bgp_namespace="", vtysh_shell_cmd=constants.RVTYSH_COMMAND): if vtysh_cmd == "show ip route vrf all static": return config_int_ip_common.show_ip_route_with_static_expected_output elif vtysh_cmd == "show ipv6 route vrf all static": @@ -217,7 +218,7 @@ def setup_multi_asic_bgp_instance(request): m_asic_json_file = os.path.join( test_path, 'mock_tables', 'dummy.json') - def mock_run_bgp_command_for_static(vtysh_cmd, bgp_namespace=""): + def mock_run_bgp_command_for_static(vtysh_cmd, bgp_namespace="", vtysh_shell_cmd=constants.RVTYSH_COMMAND): if bgp_namespace != 'test_ns': return "" if vtysh_cmd == "show ip route vrf all static": @@ -227,7 +228,7 @@ def mock_run_bgp_command_for_static(vtysh_cmd, bgp_namespace=""): else: return "" - def mock_run_bgp_command(vtysh_cmd, bgp_namespace): + def mock_run_bgp_command(vtysh_cmd, bgp_namespace, vtysh_shell_cmd=constants.RVTYSH_COMMAND): if m_asic_json_file.startswith('bgp_v4_network') or \ m_asic_json_file.startswith('bgp_v6_network'): return mock_show_bgp_network_multi_asic(m_asic_json_file) diff --git a/utilities_common/bgp_util.py b/utilities_common/bgp_util.py index 1d46ec568639..c0262b3aeb9a 100644 --- a/utilities_common/bgp_util.py +++ b/utilities_common/bgp_util.py @@ -173,16 +173,15 @@ def get_neighbor_dict_from_table(db, table_name): return neighbor_dict -def run_bgp_command(vtysh_cmd, - bgp_namespace=multi_asic.DEFAULT_NAMESPACE): +def run_bgp_command(vtysh_cmd, bgp_namespace=multi_asic.DEFAULT_NAMESPACE, vtysh_shell_cmd=constants.VTYSH_COMMAND): bgp_instance_id = ' ' output = None if bgp_namespace is not multi_asic.DEFAULT_NAMESPACE: bgp_instance_id = " -n {} ".format( multi_asic.get_asic_id_from_name(bgp_namespace)) - cmd = 'sudo vtysh {} -c "{}"'.format( - bgp_instance_id, vtysh_cmd) + cmd = 'sudo {} {} -c "{}"'.format( + vtysh_shell_cmd, bgp_instance_id, vtysh_cmd) try: output = clicommon.run_command(cmd, return_cmd=True) except Exception: @@ -191,6 +190,9 @@ def run_bgp_command(vtysh_cmd, return output +def run_bgp_show_command(vtysh_cmd, bgp_namespace=multi_asic.DEFAULT_NAMESPACE): + return run_bgp_command(vtysh_cmd, bgp_namespace, constants.RVTYSH_COMMAND) + def get_bgp_summary_from_all_bgp_instances(af, namespace, display): device = multi_asic_util.MultiAsic(display, namespace) @@ -205,7 +207,7 @@ def get_bgp_summary_from_all_bgp_instances(af, namespace, display): bgp_summary = {} cmd_output_json = {} for ns in device.get_ns_list_based_on_options(): - cmd_output = run_bgp_command(vtysh_cmd, ns) + cmd_output = run_bgp_show_command(vtysh_cmd, ns) try: cmd_output_json = json.loads(cmd_output) except ValueError: diff --git a/utilities_common/constants.py b/utilities_common/constants.py index ee2a23c98dbf..536965d0099b 100644 --- a/utilities_common/constants.py +++ b/utilities_common/constants.py @@ -8,4 +8,5 @@ PORT_OBJ = 'PORT' IPV4 = 'v4' IPV6 = 'v6' - +VTYSH_COMMAND = 'vtysh' +RVTYSH_COMMAND = 'rvtysh'