繁体   English   中英

Netmiko 和 Textfsm 路径和环境问题 Windows

[英]Netmiko and Textfsm Path and Env Issue Windows

我目前正在尝试在 Windows 10 中使用 Netmiko 和 Textfsm 设置测试,但是无论我尝试设置 textfsm 环境变量的路径是什么,它仍然无法拾取它并引发错误:

未找到有效的 ntc-templates,请安装https://github.com/networktocode/ntc-templates然后将 NET_TEXTFSM 环境变量设置为指向 ./ntc-templates/templates 目录。

我尝试通过系统属性-> 环境变量手动设置环境变量,但仍然得到相同的消息。 我尝试了绝对路径和相对路径,没有 go。 理想情况下,模板文件夹的相对路径将始终与调用它的脚本并排。 这可能很简单,但我现在完全想念它。

文件夹结构:

在此处输入图像描述

我的代码:

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 存储库所有者的帮助下解决了问题:

下面的代码适用于以下库和版本:

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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