繁体   English   中英

使用textFSM解析多个show命令

[英]Using textFSM to parse multiple show commands

我正在尝试使用 textFSM 编译一些瞻博网络“显示”命令输出并从中获取特定字段,最后一次运行打印收集的信息。 这是我的代码:

 import textfsm import getpass from.netmiko import ConnectHandler from.netmiko.ssh_exception import NetMikoTimeoutException, NetMikoAuthenticationException import os def enable.netconf(remote_device): junos_device = ConnectHandler(**remote_device) command_2 = junos_device.send_command("show interfaces") junos_device.disconnect() def main(): print("Enter Device info to check:\n") tuser = input("Enter username: ") tpw = getpass.getpass() with open("D:\Documents\sample3.csv", encoding='utf-8') as tf: for line in tf: my_ip = line.rstrip(os.linesep) remote_device = { 'device_type': 'juniper', 'ip': my_ip, 'username': tuser, 'password': tpw, } enable.netconf(remote_device) with open("D:\Documents\juniper_junos_show_interfaces.textsm", "r") as f: template = textfsm.TextFSM(f) result = template.ParseText(command_2) print(result) if __name__ == '__main__': main()

我使用 Netmiko 连接到 Juniper vMX 设备。 我还从此链接 ( https://github.com.networktocode/ntc-templates/blob/master/ntc_templates/templates/juniper_junos_show_interfaces.textfsm ) 下载 textFSM“显示界面”神庙,并将它们保存在 D:\Documents 文件夹中。

首先,我需要使 textFSM 的基本 function 正常工作。 在上面的代码中,我收到错误消息,指出“command_2”变量尚未定义,正如我在“def enable.netconf(remote_device)”中定义的那样。

你能帮我解决这个问题吗,因为我是 Python 的新手。谢谢。

如果你想在不同的 def 中使用一个变量,你可以使用 global 命令。 在此示例中,只需将“global comman_2”放在 def enable.netconf(remote_device) 下方:

def enable.netconf(远程设备):

global command_2
junos_device = ConnectHandler(**remote_device)
command_2 = junos_device.send_command("show interfaces")
junos_device.disconnect()

暂无
暂无

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

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