0

请我完全是这个 python 编程的新手。我一直在努力将渲染配置推送到 GNS3 中的 64 个路由器,但没有成功。我会在这里感谢您的帮助。

  1. 我成功渲染了配置并可以在屏幕上打印出来。

  2. 我在这里使用了 Yaml 模板和 jinja2 模板

不知道如何将渲染结果推送到 GNS3 上配置的设备。

使用的 Python 代码:

import yaml
from jinja2 import Template
from netmiko import Netmiko
import netmiko
import json
from netmiko import ConnectHandler
from getpass import getpass

username = input('Enter your SSH username: ')
password = getpass()    

#read your yaml file
with open("Just4testing8.yml") as file:
    devices = yaml.safe_load(file)

#read your jinja template file
with open("Just4testing7.j2") as file:
    template = Template(file.read())   
    
for device in devices["devices"]:

    final = template.render(
    device=device["name"],
    interfaces=device["interfaces"],
    bgpasn=device["bgpasn"],
    bgp_id=device["bgp_id"],
    bgp_neighbors=device["bgp_neighbors"])
    print(final)
4

1 回答 1

0

看起来您缺少您的 netmiko 代码。我想这就是你要找的。

这是一个使用命令列表的基本示例,我假设您会将渲染配置解析为该列表。我对渲染一无所知,但看起来像一本字典,所以你可以创建一个值列表。

** 样品 -- 未经测试**

from netmiko import ConnectHandler
from getpass import getpass

password = getpass()

cisco1 = {
    "device_type": "cisco_ios",
    "host": "192.168.255.1",
    "username": "user",
    "password": password,
}

cisco2 = {
    "device_type": "cisco_ios",
    "host": "192.168.1.1",
    "username": "user",
    "password": password,
}

devices = [cisco1, cisco2]
# you can use a your render here to build your list. 
commands = ["cmd1","cmd2","cmd3"]

for device in devices:
    net_connect = ConnectHandler(**device)
    for cmd in commands: 
        output = net_connect.send_command(cmd) # works with show commands only
        print(output) # Save to a file or append to to list/dictionary for use later. 

    net_connect.disconnect() 
于 2021-04-17T00:02:31.797 回答