[python]TextFSM and TTP

I was testing on another command show run object network, and ttp did not match the word “description” and hence the part about “description” was omitted. This is not a problem as netmiko already has a ready made textFSM template that matches show run object network.

Example of ttp with show run object network

This is the object configuration in fw02:
nor32

The ttp template:

object network {{network_object_name}}
 host {{ip_address}}
 description {{desc}}

object network {{network_object_name}}
 subnet {{subnet}} {{netmask}}
 description {{desc}}

object network {{network_object_name}}
 range {{start_ip}} {{end_ip}}
 description {{desc}}

The regex in ttp will match all three output patterns, but description was missed out.
nor33

If description is not important then it is ok. but if it is important then use netmiko’s textFSM, it has a textFSM template made for show run object network, if the output is not satisfactory you can modify the template configuration yourself or create your own.

Netmiko with TextFSM for show run object network

If you only installed netmiko then call netmiko directly:

from netmiko import ConnectHandler
from pyvault2.vault.hvault2 import get_kv2_secret
from pprint import pprint


vault_data = get_kv2_secret(mount_path="cisco_asa", path="fw02", find="data")
vault_data.update({"device_type": "cisco_asa"})

with ConnectHandler(**vault_data) as conn:
    result = conn.send_command("show run object network", use_textfsm=True)

pprint(result)

If you installed nornir then use nornir to call netmiko_send_command.

from nornir.plugins.tasks.networking import netmiko_send_command
from network.ciscoasa import connect_asa_host
from pprint import pprint


fw02 = connect_asa_host(hostname="fw02")
response = fw02.run(task=netmiko_send_command, command_string="show run object network", use_textfsm=True)
pprint(response["fw02"][0].result)

The output will be the same:

[{'cidr': '',
  'desc': 'Test object',
  'end_ip': '',
  'host': '192.168.1.1',
  'mask': '',
  'name': 'centos',
  'network': '',
  'start_ip': '',
  'type': 'host'},
 {'cidr': '',
  'desc': 'Test object',
  'end_ip': '192.168.1.120',
  'host': '',
  'mask': '',
  'name': 'host1',
  'network': '',
  'start_ip': '192.168.1.100',
  'type': 'range'},
 {'cidr': '',
  'desc': 'test subnet',
  'end_ip': '',
  'host': '',
  'mask': '255.255.255.0',
  'name': 'test2',
  'network': '192.168.1.0',
  'start_ip': '',
  'type': 'subnet'},
 {'cidr': '',
  'desc': 'test host',
  'end_ip': '',
  'host': '192.168.1.100',
  'mask': '',
  'name': 'test3',
  'network': '',
  'start_ip': '',
  'type': 'host'}]

3 thoughts on “[python]TextFSM and TTP

  1. ‘description {{ description | PHRASE }}’ worked whenever the description contain spaces.
    Awesome articles by the way, keep bumping on your articles for most of my network automation searches.
    Thanks.

  2. Thank you, I hope the articles helped 🙂 I recorded them in a blog so that I can refer them again as I forgot things which I do not always do.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s