Netmiko和Textfsm路径和环境问题窗口

2024-09-27 17:34:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在尝试在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.

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

文件夹结构:

enter image description here

我的代码:

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所需的所有信息


Tags: thefromimportipnetosshow环境变量
1条回答
网友
1楼 · 发布于 2024-09-27 17:34:39

在netmiko存储库所有者的帮助下解决了问题:

以下代码适用于以下LIB和版本:

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)

相关问题 更多 >

    热门问题