TextFSM

pip list
pip install txtfsm
pip install colorama

Install the template for NTC
git clone https://github.com/networktocode/ntc-templates
export NET_TEXTFSM=/path/to/ntc-templates/templates/

#ConnectHandler
from netmiko import Netmiko
from getpass import getpass

password = ‘cisco’

devices = {‘host’: ‘100.96.0.18’, ‘username’: ‘ntc’, ‘password’: ‘ntc123’, ‘dev>
net_conn = Netmiko(**devices)
net_conn.enable()

output = net_conn.send_command(“show ip int brief”, use_textfsm=True)
print(output)
print(type(output))

Another Example :

from netmiko import Netmiko

from getpass import getpass
from pprint import pprint

#password = getpass()
devices = {‘host’: ‘XXXXX’, ‘username’: ‘XXXXX’, ‘password’: ‘XXXXX’, ‘device_type’: ‘cisco_ios’, ‘session_log’: ‘logging.txt’}

ssh_conn = Netmiko(**devices)
print(ssh_conn.find_prompt())

show_version = ssh_conn.send_command(‘show version’, use_textfsm=True)
show_lldp = ssh_conn.send_command(‘show lldp neighbor’, use_textfsm=True)

print(f”The device {devices[‘host’]} is connected to port {show_lldp[0][‘neighbor_interface’]}”)

To find what template are available :

cat ~/ntc-templates/templates/index | grep “enter the device type here”

 

Reference :
Python for Network Engineers

Leave a Comment