Ejemplo n.º 1
0
    def textfsm_parse_output(
            self,
            template: Union[str, TextIO, None] = None,
            to_dict: bool = True) -> Union[Dict[str, Any], List[Any]]:
        """
        Parse results with textfsm, always return structured data

        Returns an empty list if parsing fails!

        Args:
            template: string path to textfsm template or opened textfsm template file
            to_dict: convert textfsm output from list of lists to list of dicts -- basically create
                dict from header and row data so it is easier to read/parse the output

        Returns:
            structured_result: empty list or parsed data from textfsm

        Raises:
            N/A

        """
        if template is None:
            template = _textfsm_get_template(platform=self.textfsm_platform,
                                             command=self.channel_input)

        if template is None:
            return []

        template = cast(Union[str, TextIOWrapper], template)
        return textfsm_parse(
            template=template, output=self.result, to_dict=to_dict) or []
Ejemplo n.º 2
0
def test_textfsm_parse_success_string_path(parse_type):
    to_dict = parse_type[0]
    expected_result = parse_type[1]
    template = _textfsm_get_template("cisco_ios", "show ip arp")
    result = textfsm_parse(template.name, IOS_ARP, to_dict=to_dict)
    assert isinstance(result, list)
    assert result[0] == expected_result
Ejemplo n.º 3
0
    def textfsm_parse_output(self,
                             to_dict: bool = True
                             ) -> Union[Dict[str, Any], List[Any]]:
        """
        Parse results with textfsm, always return structured data

        Returns an empty list if parsing fails!

        Args:
            to_dict: convert textfsm output from list of lists to list of dicts -- basically create
                dict from header and row data so it is easier to read/parse the output

        Returns:
            structured_result: empty list or parsed data from textfsm

        Raises:
            N/A

        """
        template = _textfsm_get_template(platform=self.textfsm_platform,
                                         command=self.channel_input)
        if isinstance(template, TextIOWrapper):
            structured_result = (textfsm_parse(
                template=template, output=self.result, to_dict=to_dict) or [])
        else:
            structured_result = []
        return structured_result
Ejemplo n.º 4
0
    def textfsm_parse_output(self) -> Union[Dict[str, Any], List[Any]]:
        """
        Parse results with textfsm, always return structured data

        Returns an empty list if parsing fails!

        Args:
            N/A

        Returns:
            structured_result: empty list or parsed data from textfsm

        Raises:
            N/A

        """
        template = _textfsm_get_template(self.textfsm_platform,
                                         self.channel_input)
        if isinstance(template, TextIOWrapper):
            structured_result = textfsm_parse(template, self.result) or []
        else:
            structured_result = []
        return structured_result
Ejemplo n.º 5
0
def test_textfsm_parse_failure():
    template = _textfsm_get_template("cisco_ios", "show ip arp")
    result = textfsm_parse(template, "not really arp data")
    assert result == []
Ejemplo n.º 6
0
def test_textfsm_parse(test_data):
    to_dict, expected_output = test_data
    template = _textfsm_get_template("cisco_ios", "show ip arp")
    result = textfsm_parse(template, IOS_ARP, to_dict=to_dict)
    assert isinstance(result, list)
    assert result[0] == expected_output
Ejemplo n.º 7
0
def test_textfsm_parse_success_string_path():
    template = _textfsm_get_template("cisco_ios", "show ip arp")
    result = textfsm_parse(template.name, IOS_ARP)
    assert isinstance(result, list)
    assert result[0] == ["Internet", "172.31.254.1", "-", "0000.0c07.acfe", "ARPA", "Vlan254"]
Ejemplo n.º 8
0
def test_textfsm_parse_url_path(test_data):
    to_dict, expected_output = test_data
    template = IOS_ARP_NTC_TEMPLATE_URL
    result = textfsm_parse(template, IOS_ARP, to_dict=to_dict)
    assert isinstance(result, list)
    assert result[0] == expected_output