简体   繁体   中英

Netmiko and Textfsm Path and Env Issue Windows

I am currently trying to set up a test with Netmiko and Textfsm in Windows 10, but no matter what path I try to setup the textfsm environment variable, it still doesn't pick it up and throws an error:

Valid ntc-templates not found, please install https://github.com/networktocode/ntc-templates and then set the NET_TEXTFSM environment variable to point to the./ntc-templates/templates directory.

I tried setting the environment variable manually via system properties --> environment variables, but still get the same message. I tried absolute as well as relative paths and no go. Ideally a relative path as the template folder will alway be alongside the script calling it. It might be something simple but im totally missing it right now.

The folder structure:

在此处输入图像描述

My Code:

import os, json
from netmiko import Netmiko
from netmiko import NetMikoAuthenticationException

templates = os.path.dirname(os.path.abspath(__file__)) + '\\ntc-template\\templates\\'
os.environ['NET_TEXTFSM']= templates
print(os.environ['NET_TEXTFSM'])

###############################################################
#Can i set the env var from within the scirpt using python?
#os.system(f'cmd /c "set NET_TEXTFSM={templates}"')
###############################################################

switch = {'device_type': 'cisco_ios',
            'ip': '192.168.0.20',
            'username': 'cisco',
            'password': 'cisco',
            'secret': 'cisco',
            'timeout': 10000,
            'session_timeout': 10000}

try:
    c = Netmiko(**switch)
    c.enable()
    show_ip_arp = c.send_command('show ip arp', use_textfsm=True)
    print(json.dumps(show_ip_arp))
except Exception as e:
    print(e)

I was hoping anyone points to what might be wrong or missing. I would love to avoid having to set up any environment variables via cmd unless it can be automated as well. The idea is whoever opens this py file gets all that's needed to use textfsm.

Issue resolved with the help of the netmiko repository owner:

The code below works with the following libs and version:

netmiko==3.1.0
ntc-templates==1.4.0
textfsm==1.1.0
import os, json
import ntc_templates
from netmiko import Netmiko
from netmiko import NetMikoAuthenticationException

switch = {'device_type': 'cisco_ios',
            'ip': '192.168.0.20',
            'username': 'cisco',
            'password': 'cisco',
            'secret': 'cisco',
            'timeout': 10000,
            'session_timeout': 10000}

try:
    c = Netmiko(**switch)
    c.enable()
    show_ip_arp = c.send_command('show ip arp', use_textfsm=True)
    print(show_ip_arp)
except Exception as e:
    print(e)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM