Exemple #1
0
def parse_command_dynamic(
    command_output, attributes_dict, index_file="index", templ_path="templates"
):

    cli_table = clitable.CliTable(index_file, templ_path)
    cli_table.ParseCmd(command_output, attributes_dict)
    return [dict(zip(cli_table.header, row)) for row in cli_table]
Exemple #2
0
def send_and_parse_show_command(device_dict,
                                command,
                                templates_path,
                                index='index'):
    '''
   '''
    with ConnectHandler(**device_dict) as ssh:
        ssh.enable(
        )  #помнить что некоторые команды можно выполнять и без en режима
        result = ssh.send_command(command)
        ip = device_dict['ip']
        print('connect to device {}'.format(ip))


#        print(result)
    attributes_dict = {'Command': 'sh ip int brief', 'Vendor': 'cisco_ios'}
    attributes_dict['Command'] = command
    cli = clitable.CliTable(index, templates_path)
    cli.ParseCmd(result, attributes_dict)
    #print('Formatted Table:\n', cli.FormattedTable())
    data_rows = [list(row) for row in cli]  #разобратся
    header = list(cli.header)
    #print(header)
    #print(data_rows)
    list_result = []
    for data in data_rows:
        dict_temp = {}
        for i in range(4):
            dict_temp[header[i]] = data[i]
        list_result.append(dict_temp)
    return list_result
Exemple #3
0
def send_and_parse_command_parallel(device, command, templates_path, index='index'):

	start_msg = '===> {} Connection: {}'
	received_msg = '<=== {} Received:   {}'
	ip = device['host']
	logging.info(start_msg.format(datetime.now().time(), ip))

	with ConnectHandler(**device) as ssh:
		ssh.enable()
		output = ssh.send_command(command)
		logging.info(received_msg.format(datetime.now().time(), ip))

	cli_table = clitable.CliTable(index, templates_path)
	cli_table.ParseCmd(output, attributes)
	header = list(cli_table.header)

	data_rows = [list(row) for row in cli_table]
	header = list(cli_table.header)

	final = []
	intf, address, status, protocol = header
	for row in data_rows:
		final2 = {}
		intf1, address1, status1, protocol1 = row
		final2[intf] = intf1
		final2[address] = address1
		final2[status] = status1
		final2[protocol] = protocol1
		final.append(final2)
	return final
Exemple #4
0
def parse_output(platform=None, command=None, data=None):
    """Return the structured data based on the output from a network device."""

    if not HAS_CLITABLE:
        msg = """
The TextFSM library is not currently supported on Windows. If you are NOT using Windows
you should be able to 'pip install textfsm' to fix this issue. If you are using Windows
then you will need to install the patch referenced here:

https://github.com/google/textfsm/pull/82

"""
        raise ImportError(msg)

    template_dir = _get_template_dir()
    cli_table = clitable.CliTable("index", template_dir)

    attrs = {"Command": command, "Platform": platform}
    try:
        cli_table.ParseCmd(data, attrs)
        structured_data = _clitable_to_dict(cli_table)
    except clitable.CliTableError as e:
        raise Exception(
            'Unable to parse command "{0}" on platform {1} - {2}'.format(
                command, platform, str(e)))
        # Invalid or Missing template
        # module.fail_json(msg='parsing error', error=str(e))
        # rather than fail, fallback to return raw text
        # structured_data = [data]

    return structured_data
Exemple #5
0
def fsm_parse(mls, cmd="show ip ospf database router", platform="cisco_ios"):
    """
    Runs CLITable for given mls, using the given command, for the given platform
    :param mls: Multi-line string containing Cisco IOS CLI output for a command
    :param cmd: Command to run CLITable with
    :param platform: Platform to rune CLITable with
    :return: Structured Data (DML-Ready) return from CLITable
    """
    def clitable_to_dict(cli_tbl) -> list:
        """
        Sourced from github.com/ntc-templates
        :param cli_tbl: CLITable object
        :return: Structured Data (DML-Ready)
        """
        objs = []
        for row in cli_tbl:
            temp_dict = {}
            for index, element in enumerate(row):
                temp_dict[cli_tbl.header[index].lower()] = element
            objs.append(temp_dict)

        return objs

    # Index is a file that exists in the same directory as the FSMTemplate templates
    cli_table = clitable.CliTable("index", '.')
    attrs = {'Command': cmd, 'platform': platform}

    # Inline regex removes IOS shell prompt if seen on a line e.g. "hostname# "
    cli_table.ParseCmd(re.sub(r".*#.*\n", '', mls), attrs)

    return clitable_to_dict(cli_table)
Exemple #6
0
 def send_show_command(self, command, parse, templates):
     #отправляем команду show НЕ в конфигурационном режиме
     self.telnet.write(
         b"end\n"
     )  #выходим из режима конфиг в который попали при инициализации __init__
     time.sleep(0.5)
     self.telnet.write(command.encode("ascii") + b"\n")
     time.sleep(1)
     self.telnet.write(
         b"conf t \n"
     )  #возвращаемся в конфиг режим не уверен нужна команда ли нет
     output = self.telnet.read_very_eager().decode("ascii")
     if parse == True:
         attributes_dict = {'Vendor': 'cisco_ios'}
         attributes_dict['Command'] = command
         cli = clitable.CliTable('index', templates)
         cli.ParseCmd(output, attributes_dict)
         data_rows = [list(row) for row in cli]  # разобратся
         header = list(cli.header)
         list_result = []
         for data in data_rows:
             dict_temp = {}
             for i in range(4):
                 dict_temp[header[i]] = data[i]
             list_result.append(dict_temp)
         output = list_result
     return output
Exemple #7
0
 def send_show_command(self,
                       str_cmd,
                       parse=False,
                       templates='templates',
                       index='index'):
     r = {}
     self._write_line(str_cmd)
     if not parse:
         output = self.telnet.read_until(b"#", timeout=5).decode("utf-8")
         return output
     else:
         result = []
         cli_table = clitable.CliTable('index', 'templates')
         attributes = {
             'Command': 'show ip interface brief',
             'Vendor': 'Cisco'
         }
         output = self.telnet.read_until(b"#", timeout=5).decode("utf-8")
         cli_table.ParseCmd(output, attributes)
         result = [
             dict(zip(cli_table.header, v))
             for v in [list(row) for row in cli_table]
         ]
         """
         for ll in data_rows:
             d = {}
             for idx, v in enumerate(ll):
                     d[cli_table.header[idx]] = v
             result.append(d)
         """
         #result = [ dict(zip(cli_table.header, v)) for v in data_rows ]
         return result
Exemple #8
0
def parse_command_output(template, command_output):
    """Return list() = [[headers], [processed_out], ..]"""
    with open(template) as ft:
        fsm = textfsm.TextFSM(ft)
    cli_table = clitable.CliTable('index', 'templates')
    processed_output = fsm.ParseText(command_output)
    return [fsm.header] + processed_output
Exemple #9
0
def parse_command_dynamic(command_output, attributes_dict, index_file,
                          templ_path):
    result = []
    cli_t = clitable.CliTable(index_file, templ_path)
    cli_t.ParseCmd(command_output, attributes_dict)
    for c in cli_t:
        result.append(dict(zip(list(cli_t.header), list(c))))
    pprint(result)
Exemple #10
0
def parse_command_dynamic(command_output,
                          attributes_dict,
                          index_file="index",
                          templ_path="templates"):
    cli = clitable.CliTable(index_file, templ_path)
    cli.ParseCmd(command_output, attributes_dict)
    header = list(cli.header)
    return [dict(zip(header, list(res))) for res in cli]
Exemple #11
0
def parse_command_dynamic(command_output,
                          attributes_dict,
                          index_file='index',
                          templ_path='templates'):
    ct = clitable.CliTable(index_file, templ_path)
    ct.ParseCmd(command_output, attributes_dict)
    header = (ct.header)
    return [dict(zip(header, e)) for e in ct]
Exemple #12
0
 def testCompletion(self):
   """Tests '[[]]' syntax replacement."""
   indx = clitable.CliTable()
   self.assertEqual('abc', re.sub(r'(\[\[.+?\]\])', indx._Completion, 'abc'))
   self.assertEqual('a(b(c)?)?',
                    re.sub(r'(\[\[.+?\]\])', indx._Completion, 'a[[bc]]'))
   self.assertEqual('a(b(c)?)? de(f)?',
                    re.sub(r'(\[\[.+?\]\])', indx._Completion,
                           'a[[bc]] de[[f]]'))
Exemple #13
0
def parse_command_dynamic(command_output,
                          attributes_dict,
                          index_file='index',
                          templ_path='templates'):
    cli_table = clitable.CliTable(index_file, templ_path)
    cli_table.ParseCmd(command_output, attributes_dict)
    res_lst = [list(row) for row in cli_table]
    header_lst = list(cli_table.header)
    return [(dict(zip(header_lst, fsm_el))) for fsm_el in res_lst]
def parse_command_dynamic(command_output,
                          attributes_dict,
                          index_file='index',
                          templ_path='templates'):
    cli_table = clitable.CliTable(index_file, templ_path)
    cli_table.ParseCmd(command_output, attributes_dict)

    headers = list(cli_table.header)
    return [dict(zip(headers, row)) for row in cli_table]
Exemple #15
0
 def _parse_output(self, command, command_output, templates_dir="templates"):
     attributes = {"Command": command, "Vendor": "cisco_ios"}
     cli = clitable.CliTable("index", templates_dir)
     cli.ParseCmd(command_output, attributes)
     parsed_output = [dict(zip(cli.header, row)) for row in cli]
     if parsed_output:
         return parsed_output
     else:
         return command_output
Exemple #16
0
 def send_show_command(self, command, parse=False, templates='templates/'):
     if not parse:
         return self._write_line(command).decode('utf-8')
     command_output = self._write_line(command).decode('utf-8')
     cli_table = clitable.CliTable('index', templates)
     attributes_dict = {'Command': command}
     cli_table.ParseCmd(command_output, attributes_dict)
     res_lst = [list(row) for row in cli_table]
     header_lst = list(cli_table.header)
     return [(dict(zip(header_lst, fsm_el))) for fsm_el in res_lst]
Exemple #17
0
 def send_show_command(self, command, parse=True, templates="templates"):
     self._write_line(command)
     time.sleep(1)
     command_output = self.telnet.read_very_eager().decode("ascii")
     if not parse:
         return command_output
     attributes = {"Command": command, "Vendor": "cisco_ios"}
     cli = clitable.CliTable("index", templates)
     cli.ParseCmd(command_output, attributes)
     return [dict(zip(cli.header, row)) for row in cli]
def parse_command_dynamic(command_output,
                          attributes_dict,
                          index_file='index',
                          templ_path='templates'):
    cli_table = clitable.CliTable(index_file, templ_path)
    cli_table.ParseCmd(command_output, attributes_dict)
    header = list(cli_table.header)
    data_rows = [list(row) for row in cli_table]
    output_list = [dict(zip(header, r)) for r in data_rows]
    return output_list
Exemple #19
0
def parse_output(command, output, vendor=None):
    cli = clitable.CliTable("index", "templates")
    attributes = {"Command": command}
    if vendor:
        attributes = {"Command": command, "Vendor": vendor}

    cli.ParseCmd(output, attributes)

    data_rows = [list(row) for row in cli]
    return data_rows
Exemple #20
0
def send_and_parse_show_command(device_dict, command, templates_path='templates'):
    """
    The function connects to device, than sent command show with netmiko, then  parsed command output with TextFSM
    :param device_dict: list of dictionaries with parameters of connections to devices (from *.yaml)
    :param command: the command to be executed
    :param templates_path: path to templates TextFSM. The default "templates"
    :return: result is list of dictionaries from command output (ex. task_22_1a):
        * keys - name of the variables in templates TextFSM
        * values - are parts of the command output that correspond to variables
    """
    start_msg = '===> {} Connection TO: {}'
    received_msg = '<=== {} Received FROM:  {}'
    time_of_parse_msg = 'The time of parsing the command from the device {} was: {}'

    result = []
    ip = device_dict['ip']
    vendor = device_dict['device_type']

    start_time = datetime.now()
    logging.info(start_msg.format(datetime.now().time(), ip))

    # Connect to device and return command output
    with ConnectHandler(**device_dict) as ssh:
        ssh.enable()
        # host_name = ssh.find_prompt()
        command_output = ssh.send_command(command)
        # print('command_output: \n', command_output)
        logging.info(received_msg.format(datetime.now().time(), ip))

    # parsing result of command_output (with TextFSM)
    attributes = {'Command': command, 'Vendor': vendor}
    cli_table = clitable.CliTable('index', templates_path)
    cli_table.ParseCmd(command_output, attributes)

    # Some variants for print parsed command output
    # print('CLI Table output:\n', cli_table)

    print('Formatted Table:\n', cli_table.FormattedTable())

    # Lists of strings parsed command output
    # data_rows = [list(row) for row in cli_table]
    header = list(cli_table.header)

    # print('-' * 79)
    # print('List of lists:\n', header)
    # for row in data_rows:
    #     print(row)
    # print('-' * 79)

    # Make result - list of dictionaries parsed command output
    for item in cli_table:
        result_dict = dict(zip(header, item))
        result.append(result_dict)
    logging.info(time_of_parse_msg.format(ip, (datetime.now() - start_time)))
    return result
Exemple #21
0
def get_structured_data_textfsm(
    raw_output: str,
    platform: Optional[str] = None,
    command: Optional[str] = None,
    template: Optional[str] = None,
) -> Union[str, List[Dict[str, str]]]:
    """
    Convert raw CLI output to structured data using TextFSM template.

    You can use a straight TextFSM file i.e. specify "template". If no template is specified,
    then you must use an CliTable index file.
    """
    if platform is None or command is None:
        attrs = {}
    else:
        attrs = {"Command": command, "Platform": platform}

    if template is None:
        if attrs == {}:
            raise ValueError(
                "Either 'platform/command' or 'template' must be specified.")
        template_dir = get_template_dir()
        index_file = os.path.join(template_dir, "index")
        textfsm_obj = clitable.CliTable(index_file, template_dir)
        output = _textfsm_parse(textfsm_obj, raw_output, attrs)

        # Retry the output if "cisco_xe" and not structured data
        if platform and "cisco_xe" in platform:
            if not isinstance(output, list):
                attrs["Platform"] = "cisco_ios"
                output = _textfsm_parse(textfsm_obj, raw_output, attrs)
        return output
    else:
        template_path = Path(os.path.expanduser(template))
        template_file = template_path.name
        template_dir_alt = template_path.parents[0]
        # CliTable with no index will fall-back to a TextFSM parsing behavior
        textfsm_obj = clitable.CliTable(template_dir=template_dir_alt)
        return _textfsm_parse(textfsm_obj,
                              raw_output,
                              attrs,
                              template_file=template_file)
Exemple #22
0
def parse_command_dynamic(command_output,
                          attributes_dict,
                          index_file="index",
                          templ_path="templates"):
    cli_table = clitable.CliTable(index_file, templ_path)
    cli_table.ParseCmd(command_output, attributes_dict)
    data_list = [list(row) for row in cli_table]
    result = []
    for line in data_list:
        result.append(dict(zip(cli_table.header, line)))
    return result
def get_structured_data(raw_output, platform, command):
    """Convert raw CLI output to structured data using TextFSM template."""
    template_dir = get_template_dir()
    index_file = "index"  # CHANGED
    textfsm_obj = clitable.CliTable(index_file, template_dir)
    attrs = {"Command": command, "Platform": platform}
    try:
        # Parse output through template
        textfsm_obj.ParseCmd(raw_output, attrs)
        return clitable_to_dict(textfsm_obj)
    except CliTableError:
        return raw_output
Exemple #24
0
 def send_show_command(self, sh_command, parse=None, templates='templates'):
     self._write_line(sh_command)
     time.sleep(1)
     out = self.t.expect([b'[>#]'])[2].decode('cp866')
     if parse:
         cli = clitable.CliTable('index', templates)
         attributes = {'Command': sh_command, 'Vendor': 'cisco_ios'}
         cli.ParseCmd(out, attributes)
         header = list(cli.header)
         return [dict(zip(header, list(res))) for res in cli]
     else:
         return out
def get_structured_data(raw_output, platform, command):
    """Convert raw CLI output to structured data using TextFSM template."""
    template_dir = get_template_dir()
    index_file = os.path.join(template_dir, 'index')
    textfsm_obj = clitable.CliTable(index_file, template_dir)
    attrs = {'Command': command, 'Platform': platform}
    try:
        # Parse output through template
        textfsm_obj.ParseCmd(raw_output, attrs)
        return clitable_to_dict(textfsm_obj)
    except CliTableError:
        return raw_output
Exemple #26
0
def parse_command_dynamic(command_output,
                          attributes_dict,
                          index_file='index',
                          templ_path='templates'):
    """Return list of dict as result of task_21.1a"""
    cli_table = clitable.CliTable(index_file, templ_path)
    cli_table.ParseCmd(command_output, attributes_dict)
    result = []
    for values in cli_table:
        if not(cli_table[0] in values):
            result.append(dict(zip(cli_table[0], values)))
    return result
Exemple #27
0
def send_and_parse_show_command(device_dict, command, templates_path):
    logging.info(f'Connecting to {device_dict["ip"]}...')
    with netmiko.ConnectHandler(**device_dict) as ssh:
        ssh.enable()
        prompt = ssh.find_prompt()[:-1]
        command_output = ssh.send_command(command)
    cli_table = clitable.CliTable('index', templates_path)
    attributes_dict = {'Command':command}
    cli_table.ParseCmd(command_output, attributes_dict)
    res_lst =  [list(row) for row in cli_table]
    header_lst = list(cli_table.header)
    return {prompt:[(dict(zip(header_lst, fsm_el))) for fsm_el in res_lst]}
Exemple #28
0
def parse_output(command, data, platform=None):
    template_dir = Path(__file__).parent / 'templates'
    cli_table = clitable.CliTable("index", template_dir)

    attrs = {"Command": command}
    if platform:
        attrs.update({"Platform": platform})
    cli_table.ParseCmd(_strip_prefix(data), attrs)
    header = [h.lower() for h in cli_table.header]
    parsed_dict = [dict(zip(header, line)) for line in cli_table]

    return parsed_dict
 def send_show_command(self, command, parse=True, templates='templates', index='index'):
     self._write_line(command)
     output = self._telnet.read_until(b"#", timeout=5).decode('utf-8')
     
     if parse:
         cli_table = clitable.CliTable(index, templates)
         attributes = {'Command': command, 'Vendor': 'cisco_ios'}
         cli_table.ParseCmd(output, attributes)
         
         headers = list(cli_table.header)
         return [dict(zip(headers, row)) for row in cli_table]
     else:
         return output
Exemple #30
0
 def setUp(self):
     super(UnitTestCliTable, self).setUp()
     clitable.CliTable.INDEX = {}
     self.clitable = clitable.CliTable('default_index', 'testdata')
     self.input_data = ('a b c\n' 'd e f\n')
     self.template = ('Value Key Col1 (.)\n'
                      'Value Col2 (.)\n'
                      'Value Col3 (.)\n'
                      '\n'
                      'Start\n'
                      '  ^${Col1} ${Col2} ${Col3} -> Record\n'
                      '\n')
     self.template_file = StringIO(self.template)