问题描述
请问我完全是这个Python编程的新手。我一直在努力将渲染的配置推送到GNS3中的64个路由器,但是没有成功。 感谢您的帮助。
-
我成功渲染了配置,并且可以在屏幕上打印出来。
-
我在这里使用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)
解决方法
看起来您缺少 netmiko 代码。我想这就是你要找的。p>
这是一个使用命令列表的基本示例,我假设您会将渲染配置解析为该列表。我对 Render 一无所知,但看起来像一本字典,因此您可以创建一个值列表。
** SAMPLE -- 未测试**
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",}
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()