我目前正在尝试在Windows 10中使用Netmiko和Textfsm设置测试,但无论我尝试设置Textfsm环境变量的路径是什么,它仍然不会拾取它并抛出错误:
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.
我尝试通过系统属性手动设置环境变量-->;环境变量,但仍然得到相同的消息。我尝试了绝对路径和相对路径,但没有成功。理想情况下,模板文件夹的相对路径将始终位于调用它的脚本旁边。这可能很简单,但我现在完全错过了
文件夹结构:
我的代码:
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)
我希望任何人都能指出可能的错误或遗漏。我希望避免通过cmd设置任何环境变量,除非它也可以自动化。这个想法是,无论谁打开这个py文件,都可以获得使用textfsm所需的所有信息
在netmiko存储库所有者的帮助下解决了问题:
以下代码适用于以下LIB和版本:
相关问题 更多 >
编程相关推荐