获取APIC-EM中的一台网络设备的配置,并将该配置保存为文件。

#get config to the switch of  cisco sandbox APIC_EM 's Rest_API information

#you can use the postman to check the https'problem or the python scripts problem

 

#!/usr/bin/python3

import requests
import json
requests.packages.urllib3.disable_warnings()
 

#the url of APIC_EM's API interface

apic_em_ip = "https://devnetapi.cisco.com/sandbox/apic_em/api/v1"

 

# post the ticket  and get the token

def get_token(url):
    api_call="/ticket"
 

#APIC_EM's user and password

    payload = { "username":"devnetuser","password":"Cisco123!"}
#header information
    header = {"content-type":"application/json"}
#url=https://devnetapi.cisco.com/sandbox/apic_em/api/v1/ticket
    url += api_call
    response = requests.post(url, data=json.dumps(payload), headers=header,verify=False).json()
    token = response["response"]["serviceTicket"]
    return token
 

#get the device_id

def get_device_id(token,url):
    api_call = "/network-device"
#post the token number
    header = {"content-type":"application/json","X-Auth-Token":token}
    url += api_call
    response = requests.get(url, headers=header, verify=False).json()
#get the device id
    for item in response["response"]:
        if item['role'] == 'ACCESS':
            return item['id']
 

def get_config(token, url, id):

    api_call = "/network-device/" + id + "/config"
    header = {"content-type":"application/json","X-Auth-Token":token}
    url += api_call
    response = requests.get(url, headers=header, verify=False).json()
    file = open('access_host_1.txt','w')
    file.write(response['response'])
    file.close()
 

 

auth_token = get_token(apic_em_ip)

device_id = get_device_id(auth_token,apic_em_ip)
get_config(auth_token, apic_em_ip,device_id)

转载于:https://www.cnblogs.com/koalabear/p/6716390.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值