Cisco API连接并应用

1.介绍
通过Python连接Catalyst API接口进行需求应用
2.下载Cisco的Python包
Command: pip3 install dnacentersdk
3.Python代码连接API
编写连接函数:


def getDNACSession(user='root', passwd='P@ssw0rd') -> api.DNACenterAPI:
"""
General function for creating session to DNAC

Default will use server side session credentials,
unless specific user/password are provided
"""
 if user and passwd:
  return api.DNACenterAPI(
     username=user,
    password=passwd,
    base_url="https://10.20.2.12/",
    verify=False,
)
# Start DNAC session
dnac = getDNACSession()
4.测试连接Catalyst Center获取设备列表


5.其它就是根据需求配置函数

6.常用函数
6.1 获取dnac中inventory的设备列表:
 用devices类中get_device_list方法
 devices = dnac.devices.get_device_list(family="Switches and Hubs")
6.2 获取设备的interface:
 devices类中get_interface_info_by_id方法,后面加设备的id
 interfaces = dnac.devices.get_interface_info_by_id('0d7fa892-5905-4209-a79e-5e2c3a152680')
6.3 获取template文件的项目及模板文件信息:
 使用configuration_templates类下面的get_projects_details方法
 dnac.configuration_templates.get_projects_details()
7.将输出的字典信息转为已读性高的格式输出:
 import json
 json.dumps(需要处理的内容,indent=1)