cisco上的自动化脚本

问题描述

是否可以使用命令运行脚本?

show int g0/0.(VLAN 列表中的变量)

我需要获取 4000 多个子接口的详细信息

谢谢

解决方法

我通常为此使用 python+netmiko

from pprint import pprint
import yaml
from netmiko import (
    ConnectHandler,NetmikoTimeoutException,NetmikoAuthenticationException,)


def send_show_command(device,commands):
    result = {}
    try:
        with ConnectHandler(**device) as ssh:
            ssh.enable()
            for command in commands:
                output = ssh.send_command(command)
                result[command] = output
        return result
    except (NetmikoTimeoutException,NetmikoAuthenticationException) as error:
        print(error)


if __name__ == "__main__":
    with open("devices.yaml") as f:
        devices = yaml.safe_load(f)
    for device in devices:
        result = send_show_command(device,["sh int g0/0",])
        pprint(result,width=120)

文件 devices.yaml:

- device_type: cisco_ios
  host: 192.168.1.1
  username: admin
  password: cisco
  secret: cisco
  port: 22
...