dmulyalin / ttp

Template Text Parser
MIT License
350 stars 34 forks source link

Error on parser netmiko output #74

Open pperesbr opened 2 years ago

pperesbr commented 2 years ago

I'm creating lib to get configuration from Huawei equipment with Netmiko and TTP.

When I run my code I receive this error message:

group.get_regexes: variable not found in line: 'dis_cur_conf_vpn_instance.ttp'

Here files with codes.

TTP file:

<group name="vpns">
ip vpn-instance {{ name | _start_ }}
 description {{ description }}
 traffic-statistics {{ traffic_statistics }}
 ipv4-family
  route-distinguisher {{ rd }}
  vpn {{ frr }}
  export route-policy {{ export_route_policy }}
  import route-policy {{ import_route_policy }}
  tnl-policy {{ tunnel_policy }}
  apply-label {{ apply_label }}
  vpn-target {{ rts_export | to_list | joinmatches }} export-extcommunity
  vpn-target {{ rts_import | to_list | joinmatches }} import-extcommunity
#{{ _end_ }}
</group>

Class python:

from pprint import pprint

from netmiko import ConnectHandler

from models import Vpn
from ssh.types import Device

class Huawei:
    device: Device
    conn: ConnectHandler

    def __init__(self, device: Device):
        self.device = device

    def connect(self) -> None:
        self.conn = ConnectHandler(device_type=self.device.type,
                                   ip=self.device.ip,
                                   host=self.device.host,
                                   username=self.device.username,
                                   password=self.device.password)

    def no_more(self) -> None:
        self.conn.send_command("screen-length 0 temporary")

    def get_vpns(self) -> None:        
        output = self.conn.send_command("display current-configuration configuration vpn-instance",
                                        use_ttp=True, ttp_template="dis_cur_conf_vpn_instance.ttp")
        pprint(output)

and original output from netmiko without ttp:

#
ip vpn-instance GER-LTE
 ipv4-family
  route-distinguisher 10429:902041026
  tnl-policy LB
  apply-label per-instance
  vpn-target 10429:16026 export-extcommunity
  vpn-target 10429:16026 import-extcommunity
#
ip vpn-instance RMG3G-TIM-VIV-IAS-IUPS-UP
 ipv4-family
  route-distinguisher 26615:36911
  tnl-policy LB
  vpn-target 26615:36910 export-extcommunity
  vpn-target 26615:30911 import-extcommunity
  vpn-target 26615:30851 import-extcommunity
  vpn-target 26615:30711 import-extcommunity
  vpn-target 26615:30611 import-extcommunity
  vpn-target 26615:30213 import-extcommunity
  vpn-target 26615:30113 import-extcommunity
#
ip vpn-instance RS-LTE-OI-TIM-PROVEDOR
 ipv4-family
  route-distinguisher 8167:9151
  tnl-policy LB
  apply-label per-instance
  vpn-target 8167:9151 export-extcommunity
  vpn-target 8167:9152 import-extcommunity
#
ip vpn-instance RS-LTE-OI-TIM-PROVEDOR-S
 ipv4-family
  route-distinguisher 8167:9153
  tnl-policy LB
  apply-label per-instance
  vpn-target 8167:9152 export-extcommunity
  vpn-target 8167:9151 import-extcommunity
#
ip vpn-instance RS-LTE-OI-TIM-TOMADOR
 ipv4-family
  route-distinguisher 8167:9101
  tnl-policy LB
  apply-label per-instance
  vpn-target 10429:16025 export-extcommunity
  vpn-target 7738:9101 export-extcommunity
  vpn-target 10429:16025 import-extcommunity
  vpn-target 7738:9102 import-extcommunity
#
ip vpn-instance RS-LTE-TIM-PROVEDOR
 ipv4-family
  route-distinguisher 26615:9151
  tnl-policy LB
  apply-label per-instance
  vpn-target 26615:9151 export-extcommunity
  vpn-target 26615:9152 import-extcommunity
#
ip vpn-instance RS-LTE-TIM-PROVEDOR-S
 ipv4-family
  route-distinguisher 26615:9153
  tnl-policy LB
  apply-label per-instance
  vpn-target 26615:9152 export-extcommunity
  vpn-target 26615:9151 import-extcommunity
#
ip vpn-instance RS-LTE-TIM-TOMADOR
 ipv4-family
  route-distinguisher 26615:9101
  tnl-policy LB
  apply-label per-instance
  vpn-target 26615:9101 export-extcommunity
  vpn-target 10429:16025 export-extcommunity
  vpn-target 26615:9102 import-extcommunity
  vpn-target 10429:16025 import-extcommunity
#
ip vpn-instance RS3G-CLR-VIV-PVD-IAS-IUPS-UP
 ipv4-family
  route-distinguisher 22085:32024
  tnl-policy LB
  vpn-target 22085:32024 export-extcommunity
  vpn-target 10429:20624 export-extcommunity
  vpn-target 26599:10 export-extcommunity
  vpn-target 22085:32024 import-extcommunity
  vpn-target 26599:10 import-extcommunity
  vpn-target 10429:20624 import-extcommunity
#
ip vpn-instance RS3G-TIM-VIV-PVD-IAS-IUPS-UP
 ipv4-family
  route-distinguisher 26615:32024
  import route-policy RS3G-TIM-VIV-PVD-FROM-SERVICE
  vpn-target 26615:32024 export-extcommunity
  vpn-target 26599:10 import-extcommunity
  vpn-target 10429:20624 import-extcommunity
  vpn-target 26615:32024 import-extcommunity
#
ip vpn-instance RS3G-TIM-VIV-TMD-IAS-IUPS-UP
 ipv4-family
  route-distinguisher 26615:31024
  import route-policy RS3G-TIM-VIV-TMD-FROM-SERVICE
  vpn-target 26615:31024 export-extcommunity
  vpn-target 26599:10 import-extcommunity
  vpn-target 10429:20624 import-extcommunity
  vpn-target 26615:31024 import-extcommunity
#
ip vpn-instance SERVICO
 ipv4-family
  route-distinguisher 10429:902041025
  tnl-policy LB
  apply-label per-instance
  vpn-target 10429:16025 export-extcommunity
  vpn-target 10429:25517 export-extcommunity
  vpn-target 10429:25519 export-extcommunity
  vpn-target 10429:16025 import-extcommunity
  vpn-target 10429:25517 import-extcommunity
  vpn-target 10429:25518 import-extcommunity
  vpn-target 10429:25519 import-extcommunity
  vpn-target 10429:25520 import-extcommunity
#
ip vpn-instance V13092:IuPS-UP
 ipv4-family
  route-distinguisher 10429:12748
  tnl-policy LB
  vpn-target 10429:20624 export-extcommunity
  vpn-target 10429:20624 import-extcommunity
  vpn-target 22085:32024 import-extcommunity
  vpn-target 26615:32024 import-extcommunity
#
ip dcn vpn-instance __dcn_vpn__
 ipv4-family
#
return

Before writed the code, I tested with https://textfsm.nornir.tech/ and that's work.

Tks,

Paulo

dmulyalin commented 2 years ago

Hi, as per docs:

        :param ttp_template: Name of template to parse output with; can be fully qualified
            path, relative path, or name of file in current directory. (default: None).
        :type ttp_template: str

do you have file "dis_cur_conf_vpn_instance.ttp" in current working directory next to your script? Might want to try passing absolute path or template content.

Also, may want to check Netmiko run_ttp method as it might make it simpler to achieve functionality your are after

pperesbr commented 2 years ago

Hi.

My file there is the same directory...

I changed to absolute path and got with one error: group.get_regexes: variable not found in line: ' ipv4-family'

group.get_regexes: variable not found in line: ' ipv4-family'
[[{'vpns': [{'apply_label': 'per-instance',
             'name': 'GER-LTE',
             'rd': '10429:902041026',
             'rts_export': ['10429:16026'],
             'rts_import': ['10429:16026'],
             'tunnel_policy': 'LB'},
            {'name': 'RMG3G-TIM-VIV-IAS-IUPS-UP',
             'rd': '26615:36911',
             'rts_export': ['26615:36910'],
             'rts_import': ['26615:30911',
                            '26615:30851',
                            '26615:30711',
                            '26615:30611',
                            '26615:30213',
                            '26615:30113'],
             'tunnel_policy': 'LB'},
            {'apply_label': 'per-instance',
             'name': 'RS-LTE-OI-VIVO-PROVEDOR',
             'rd': '8167:9151',
             'rts_export': ['8167:9151'],
             'rts_import': ['8167:9152'],
             'tunnel_policy': 'LB'},
            {'apply_label': 'per-instance',
             'name': 'RS-LTE-OI-VIVO-PROVEDOR-S',
             'rd': '8167:9153',
             'rts_export': ['8167:9152'],
             'rts_import': ['8167:9151'],
             'tunnel_policy': 'LB'},
            {'apply_label': 'per-instance',
             'name': 'RS-LTE-OI-VIVO-TOMADOR',
             'rd': '8167:9101',
             'rts_export': ['10429:16025', '7738:9101'],
             'rts_import': ['10429:16025', '7738:9102'],
             'tunnel_policy': 'LB'},
            {'apply_label': 'per-instance',
             'name': 'RS-LTE-TIM-VIVO-PROVEDOR',
             'rd': '26615:9151',
             'rts_export': ['26615:9151'],
             'rts_import': ['26615:9152'],
             'tunnel_policy': 'LB'},
            {'apply_label': 'per-instance',
             'name': 'RS-LTE-TIM-VIVO-PROVEDOR-S',
             'rd': '26615:9153',
             'rts_export': ['26615:9152'],
             'rts_import': ['26615:9151'],
             'tunnel_policy': 'LB'},
            {'apply_label': 'per-instance',
             'name': 'RS-LTE-TIM-VIVO-TOMADOR',
             'rd': '26615:9101',
             'rts_export': ['26615:9101', '10429:16025'],
             'rts_import': ['26615:9102', '10429:16025'],
             'tunnel_policy': 'LB'},
            {'name': 'RS3G-CLR-VIV-PVD-IAS-IUPS-UP',
             'rd': '22085:32024',
             'rts_export': ['22085:32024', '10429:20624', '26599:10'],
             'rts_import': ['22085:32024', '26599:10', '10429:20624'],
             'tunnel_policy': 'LB'},
            {'import_route_policy': 'RS3G-TIM-VIV-PVD-FROM-SERVICE',
             'name': 'RS3G-TIM-VIV-PVD-IAS-IUPS-UP',
             'rd': '26615:32024',
             'rts_export': ['26615:32024'],
             'rts_import': ['26599:10', '10429:20624', '26615:32024']},
            {'import_route_policy': 'RS3G-TIM-VIV-TMD-FROM-SERVICE',
             'name': 'RS3G-TIM-VIV-TMD-IAS-IUPS-UP',
             'rd': '26615:31024',
             'rts_export': ['26615:31024'],
             'rts_import': ['26599:10', '10429:20624', '26615:31024']},
            {'apply_label': 'per-instance',
             'name': 'SERVICO-LTE',
             'rd': '10429:902041025',
             'rts_export': ['10429:16025', '10429:25517', '10429:25519'],
             'rts_import': ['10429:16025',
                            '10429:25517',
                            '10429:25518',
                            '10429:25519',
                            '10429:25520'],
             'tunnel_policy': 'LB'},
            {'name': 'V13092:IuPS-UP',
             'rd': '10429:12748',
             'rts_export': ['10429:20624'],
             'rts_import': ['10429:20624', '22085:32024', '26615:32024'],
             'tunnel_policy': 'LB'}]}]]

For both ways run_ttp and send_command with ttp option.

Why I need to do this way?

Tks,

Paulo

pperesbr commented 2 years ago

The error happened because the final ouput mismatch.

ip dcn vpn-instance __dcn_vpn__
 ipv4-family
#
return
dmulyalin commented 2 years ago

error: group.get_regexes: variable not found in line: ' ipv4-family'- you seeing this error because ipv4-family line does not contain any {{ variables }}

In theory relative OS path should work as well, not sure why its notworking in your case, you can pass ttp template string as well instead of path to file.