It is a little long to install, but it is worth it : you need to install ntc-templates using a 'git clone' action. The easiest way is to just install this into your home directory.
git clone https://github.com/networktocode/ntc-templates?__s=XXXXXXXX
The index file is just a mapping between platform, command, and the corresponding TextFSM template to use. This includes possible abbreviated versions of the command (for example, 'sh ip int br' and 'show ip interface brief').
Netmiko is configured to work with ~/ntc-template/templates/index for the ntc-templates index file. I had to alter the global PATH to tell Netmiko where to look for the TextFSM template directory:
NET_TEXTFSM="/root/python/ntc-templates/templates/" >> /etc/environment
All the templates already available for parsing are located in this path. If you need to define your own, that is where you will find examples and place to begin.
Now, let's see the difference.
You might know it by know, it is pretty simple to connect to a network equipment with the Netmiko library :
#!/usr/bin/python
#from netmiko import Netmiko
core_src_switch = {
'host': 'yourswitch.domain',
'username': 'read_only_user',
'password': 'read_only_user_password',
'device_type': 'cisco_ios',
}
target_ip = 'your ip'
#opening SSH connection to device
net_conn_tr = Netmiko(**core_src_switch)
#sending show ip ARP to device
output_shiparp = net_conn.send_command("show ip arp " + target_ip)
#close SSH connection
net_conn_tr.disconnect()
print(output_shiparp)
This will have approximatively this output with a valid IP :
Protocol Address Age (min) Hardware Addr Type Interface
Internet your ip 5 cc52.aaaa.aaaa ARPA Vlan4200
And now, let's modify the command so we use TextFSM :
output_shiparp = net_conn.send_command("show ip arp " + target_ip, expect_string=r'#', use_textfsm=True)
New output :
[{'interface': 'Vlan4200', 'age': '5', 'type': 'ARPA', 'mac': 'cc52.aaaa.aaaa', 'address': 'your ip'}]
Immediately usable via code :
In [3]: output_shiparp[0]['address']
Out[3]: 'your ip'
This conclude this small presentation of textFSM. I strongly advise on using it when building code that will browse switches and routers.



