shanghaipc 发表于 2017-7-8 19:19:18

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

#get config to the switch ofcisco 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 ticketand 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)
页: [1]
查看完整版本: 获取APIC-EM中的一台网络设备的配置,并将该配置保存为文件。