Esempio n. 1
0
def get_structured_data(rawoutput, params):

    #    self.rawoutput=rawoutput
    #    self.params=params

    index_file = params['index_file']
    template_dir = params['template_dir']
    #    index_file='index'
    #    template_dir='/Users/davis.kuriakose/PycharmProjects/ntc-ansible/ntc-templates/templates'
    cli_table = clitable.CliTable(index_file, template_dir)

    attrs = dict(
        #        Command='show version',
        #        Platform='cisco_ios'
        Command=params['command'],
        Platform=params['platform'])
    try:
        cli_table.ParseCmd(rawoutput, attrs)
        structured_data = clitable_to_dict(cli_table)

    except CliTableError as e:
        # Invalid or Missing template
        # module.fail_json(msg='parsing error', error=str(e))
        # rather than fail, fallback to return raw text
        structured_data = [rawoutput]

    return structured_data
Esempio n. 2
0
    def parse_fsm(self, result, command):
        """ Parses command output through TextFSM """

        result = ''.join(result)
        cli_table = clitable.CliTable(index_file, template_dir)
        attrs = {'Command': command['command'], 'Platform': self.device_type}

        try:
            cli_table.ParseCmd(result, attrs)

            # Timestamp precision is set to 'seconds'
            timestamp = int(time())

            for field in clitable_to_dict(cli_table):
                data = {}
                data['tag'] = {
                    'host': self.hostname,
                    'command': command['tag']
                }
                data['command'] = command['command']
                data['fields'] = dict(
                    (k, float_if_possible(v)) for (k, v) in field.items())
                if command['tag']:
                    data['tag'][command['tag']] = data['fields'][
                        command['tag']]
                data['timestamp'] = timestamp
                self.data_list.append(data)

            return True

        except clitable.CliTableError as e:
            logging.error('FSM parsing error: %s' % str(e))
            return False
Esempio n. 3
0
 def reinit(self, platform=None, cli=None):
     self.root = os.path.join(os.path.dirname(__file__), '..', 'templates')
     self.samples = os.path.join(self.root, 'test')
     index_file = env.get("SPYTEST_TEXTFSM_INDEX_FILENAME", "index")
     self.cli_table = clitable.CliTable(index_file, self.root)
     self.platform = platform
     self.cli = cli
Esempio n. 4
0
    def send_commands(self):
        """ Send all commands in task
            Stores all parsed output in self.data_list
        """

        for command in self.command_list:
            logging.debug('Sending command: %s' % command['command'])
            result = self.sock.send_command(command['command'])
            result_raw = ''.join(result)

            cli_table = clitable.CliTable(index_file, template_dir)
            attrs = {
                'Command': command['command'],
                'Platform': self.device_type
            }

            try:
                cli_table.ParseCmd(result_raw, attrs)
            except CliTableError as e:
                module.fail_json(msg='parsing error', error=str(e))

            data = {}
            data['tag'] = command['tag']
            data['command'] = command['command']
            data['fields'] = clitable_to_dict(cli_table)

            # Convert values to float if possible
            data_float = []

            for i in data['fields']:
                i = dict((k, float_if_possible(v)) for (k, v) in i.items())
                data_float.append(i)
                data['fields'] = data_float

            self.data_list.append(data)
Esempio n. 5
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]
Esempio n. 6
0
def show_vlan():

    # establish ssh connection
    device = ConnectHandler(**switch)

    # define cisco ios show command
    command = 'show vlan brief'

    # Execute show commands on the channel:
    output = device.send_command(command)

    # close ssh connection gracefully
    device.disconnect()
    
    # Create CliTable object
    cli_table = clitable.CliTable(index_file, template_dir)
    attrs = {'Command': command, 'platform': device_type}

    # Dynamically parse the output from the router against the template
    cli_table.ParseCmd(output, attrs)

    # Convert cli_table to Python Dictionary
    objs = []
    for row in cli_table:
        temp_dict = {}
        for index, element in enumerate(row):
            temp_dict[cli_table.header[index].lower()] = element
        objs.append(temp_dict)

    # jsonify dictionary and return result
    return jsonify(vlans=objs)
Esempio n. 7
0
def parse_command_dynamic(att_dict,
                          output,
                          index_f='index',
                          index_dir='templates',
                          show_output=False):
    cli_table = clitable.CliTable(index_f, index_dir)
    cli_table.ParseCmd(output, att_dict)

    #print ("CLI Table output:\n", cli_table)
    if show_output:
        print("Formatted Table:\n", cli_table.FormattedTable())

    header = cli_table.header

    listd = []
    for r in cli_table:
        d = {}
        for i in range(len(header)):
            d.update({header[i]: r[i]})
        listd.append(d)

    return listd


#parse_command_dynamic(attributes,output_sh_ip_route_ospf,show_output= True)
Esempio n. 8
0
 def __init__(self, platform=None):
     """
     Construction of Template object
     """
     self.root = os.path.join(os.path.dirname(__file__), '..', 'templates')
     self.samples = os.path.join(self.root, 'test')
     self.cli_table = clitable.CliTable('index', self.root)
     self.platform = platform
Esempio n. 9
0
def send_and_parse(command,
                   command_output,
                   index_file='index',
                   templates='templates'):
    attributes = {'Command': command, 'Vendor': 'cisco_ios'}
    cli_table = clitable.CliTable(index_file, templates)
    cli_table.ParseCmd(command_output, attributes)
    return [dict(zip(cli_table.header, row)) for row in cli_table]
 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]]'))
Esempio n. 11
0
def get_structured_data(platform, command, rawoutput):
    """Return the structured data."""
    cli_table = clitable.CliTable('index', 'templates')

    attrs = dict(Command=command, Platform=platform)
    cli_table.ParseCmd(rawoutput, attrs)
    structured_data = clitable_to_dict(cli_table)

    return structured_data
Esempio n. 12
0
def initParser():
    try:
        parser = clitable.CliTable('index', TEMPLATES_DIRECTORY)
    except Exception as e:
        print(
            "Problem with parser init - check index file and templates directory. Error is {}"
            .format(e))
        return None
    return parser
Esempio n. 13
0
def parse_command_dynamic(attributes, output, index = 'index', index_dir = 'templates'):
    result = []
    cli_table = clitable.CliTable(index, index_dir)
    cli_table.ParseCmd(output, attributes)
    header = list(cli_table.header)
    data_rows = [list(row) for row in cli_table]
    for item in data_rows:
        zipped_dict = dict(zip(header, item))
        result.append(zipped_dict)
    return result
Esempio n. 14
0
 def _parse_command_dynamic(self,
                            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)
     result = [dict(zip(headers, list(row))) for row in cli_table]
     return result
Esempio n. 15
0
def parse_command_dynamic(command_output,
                          attributes_dict,
                          index_file="index",
                          templ_path="templates"):
    out_list = list()
    cli_table = clitable.CliTable(index_file, templ_path)
    cli_table.ParseCmd(command_output, attributes_dict)
    for itm in cli_table[1:]:
        out_list.append(dict(zip(cli_table[0], itm)))
    return out_list
Esempio n. 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]
Esempio n. 17
0
 def __init__(self, index_file=TEMPLATE_INDEX_FILE,
              template_dir=TEMPLATE_INDEX_DIR,
              attributes=None):
     super(CliParser, self).__init__(index_file, template_dir)
     self.template_dir = template_dir
     self.index_file = index_file
     self.attributes = attributes
     # set Version to 'DEFAULT' if it is not defined
     if 'Version' not in attributes.keys():
         self.attributes['Version'] = 'DEFAULT'
     self.cli_table = clitable.CliTable(self.index_file, self.template_dir)
Esempio n. 18
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)
    headers,tab=list(cli.header),[list(r) for r in cli] 
    result,intdict=[],{}
    for interf in tab:
        for i in range(0,len(headers)):
            intdict[headers[i]]=interf[i]
        result.append(intdict.copy())

    return result
Esempio n. 19
0
 def send_and_parse_show(self,
                         command,
                         index_file='index',
                         templates_dir='templates'):
     output = self.send_show_command(command)
     cli_table = clitable.CliTable(index_file, templates_dir)
     attributes = {'Vendor': 'huawei', 'Command': command}
     cli_table.ParseCmd(output, attributes)
     keys = list(cli_table.header)
     rows = [row for row in cli_table]
     return [{k: v for k, v in zip(keys, row)} for row in rows]
Esempio n. 20
0
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
Esempio n. 21
0
 def setUp(self):
     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 = cStringIO.StringIO(self.template)
Esempio n. 22
0
 def send_show_command(self, command, templates, parse=True):
     send = self._write_line(command)
     time.sleep(1)
     self.connection.read_until(b'#')
     output = self.connection.read_very_eager().decode('ascii')
     if parse:
         attribute = {}
         attribute['Command'] = command
         cli_table = clitable.CliTable('index', templates)
         cli_table.ParseCmd(output, attribute)
         to_return = [dict(zip(cli_table.header, row)) for row in cli_table]
         return to_return
     return output
Esempio n. 23
0
def parse_command_dynamic(attributes,
                          output,
                          index='index',
                          templates='templates'):
    cli_table = clitable.CliTable(index, templates)
    attributes = attributes

    cli_table.ParseCmd(output, attributes)

    data_rows = [list(row) for row in cli_table]
    header = list(cli_table.header)
    result = [dict(zip(header, row)) for row in data_rows]
    return result
Esempio n. 24
0
def parse_output(output, vendor, command):  #TextFsm parse output from switch
    try:
        cli_table = clitable.CliTable('index', 'templates')
    except:
        print('Warning TextFsm !!! >>>templates index error')
    # если комманда соотвествует только одному вендору то можно
    # не указывать  его
    attributes = {'Vendor': vendor, 'Command': command}
    try:
        cli_table.ParseCmd(output, attributes)
    except:
        print('Warning TextFsm !!! >>> attribute parse error')
    return cli_table
Esempio n. 25
0
def parse_command_dynamic(attrs, idx, templates, commands):
    cli_table = clitable.CliTable('index', 'templates')

    cli_table.ParseCmd(output_sh_ip_route_ospf, attributes)

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

    rr = []
    for r in data_rows:
        p = dict(zip(header, r))
        rr.append(p)
    return rr
def parse_command_dynamic(attr, output, index='index', tmpl_dir='templates'):
    result = []
    temp_dict = {}
    cli_table = clitable.CliTable(index, tmpl_dir)
    cli_table.ParseCmd(output, attr)

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

    for i in range(len(data_rows)):
        for j in range(len(header)):
            temp_dict[header[j]] = data_rows[i][j]
        result.append(temp_dict.copy())
    return result
Esempio n. 27
0
def init_parser():
    """
    Init CliTable index and templates
        This function should be call first, before parsing

        :return: cliTable object or None
    """
    try:
        parser = clitable.CliTable('index', TEMPLATES_DIRECTORY)
        logger.info("Collector module loads: Im Alive!!!")
    except Exception as e:
        logger.error("Problem with parser init - check index file and templates directory. Error is %s", e)
        return None
    return parser
Esempio n. 28
0
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)
        structured_data = clitable_to_dict(textfsm_obj)
        output = raw_output if structured_data == [] else structured_data
        return output
    except CliTableError:
        return raw_output
Esempio n. 29
0
def parse_command_dynamic(command_output, attributes_dict):

    cli = clitable.CliTable(
        'index', 'templates'
    )  # что бы работал pytest use "index", что бы скрипт работал самостоятельно "use index_file"
    cli.ParseCmd(command_output, attributes_dict)

    data_rows = [list(row) for row in cli]  # добавляем row from cli to list
    header = list(cli.header)  # add to headers to list
    data = []

    for d in data_rows:
        data.append(dict(zip(header, d)))  # долаем список словарей
    return data
Esempio n. 30
0
def send_and_parse_show_command(device_dict, command, templates_path):

    with ConnectHandler(**device_dict) as ssh:
        ssh.enable()
        result = ssh.send_command(command)

    attribute = {}
    attribute['Command'] = command

    cli_table = clitable.CliTable('index', templates_path)
    cli_table.ParseCmd(result, attribute)

    to_return = [dict(zip(cli_table.header, row)) for row in cli_table]

    return to_return