So i wanted to start learning python and enjoy the APIC-EM controller and came with this simple example of how to collect all subnets

from a controller.

 

I just realised that by default a "get" can only return 500 items, so in this new version i will collect all devices and run through their interfaces instead in one get try to collect all interfaces in the controller. In that way my little script can support collection of 500 interfaces per device and calculate ip subnets by their ip address information, remove duplicates etc..

 

Here goes with no exception handling

(i saw it failed one day cause of an ip address with letters in it.

 

This script is based on the EFT2 V0 API, so it might needs changes for the GA version.

You need arguments in command line (example) : sandboxapic.cisco.com

 

# requirements
# python install pip
# pip install git+https://github.com/tehmaze/ipcalc   (python 3 version@)
# pip install six

__author__ = 'steffen webb'

import sys

import requests

import ipcalc

import time

from operator import itemgetter

from itertools import groupby

# remove this line if not using Python 3
requests.packages.urllib3.disable_warnings()

 

def delay_print(s):

   for c in s:

  sys.stdout.write( '%s' % c )

  sys.stdout.flush()

  time.sleep(0.1)

 

if len(sys.argv) < 2:

   print('Need arguments !')

  sys.exit(1)

 

apicem_ip = sys.argv[1] # use argument 1 in command line
print("Listing Subnets used in APIC-EM controller : "+apicem_ip)

 

url = "https://" + apicem_ip + "/api/v0/network-device/count"
resp = requests.get(url, verify=False)

response_json = resp.json() # Get the json-encoded content from response with "response_json = resp.json()
count = response_json["response"]

 

interface_list = []

 

print("Collecting")

if count > 0 :

  url = "https://"+apicem_ip+"/api/v0/network-device/1/"+str(count)

  resp= requests.get(url,verify=False)

  response_json = resp.json()

   for item in response_json["response"]:

  id = item["id"]

   #print(id)
   sys.stdout.write('.')

  sys.stdout.flush()

  url = "https://" + apicem_ip + "/api/v0/interface/network-device/"+id

  resp = requests.get(url, verify=False)

  response_json = resp.json()

   for item in response_json["response"]:

   if 'ipv4Address' in item: # make sure that only interfaces with an ip address, else KeyError
   interface_list.append([item['ipv4Address'], item['ipv4Mask']])

 

 

  subnets =[]

 

   for item in interface_list:

   # print( item[0])
   if (item[0] != '') & (item[1] != ''):

  subnet = ipcalc.Network(item[0] + "/" + item[1])

  subnets.append([subnet.network().__str__().strip(),subnet.mask,subnet.network_long()])

   # print(subnet.network_long())
   print("")

 

  subnets.sort(key=itemgetter(2)) # sort by long
   print("sorting")

  delay_print("...............................")

   print("")

 

  subnets = list(map(itemgetter(0), groupby(subnets))) #remove duplicates

   print("removing duplicates")

  delay_print("...............................")

   print("")

 

   for item in subnets:

   print(item[0]+" /"+str(item[1]))

 

gives this nice simple output: python apic-fun.py sandboxapic.cisco.com

 

Listing Subnets used in APIC-EM controller : sandboxapic.cisco.com

Collecting

...................................................

sorting

...............................

removing duplicates

...............................

40.0.0.0 /24

40.0.1.0 /30

40.0.1.4 /30

40.0.1.8 /30

40.0.1.12 /30

40.0.1.16 /30

40.0.1.20 /30

40.0.1.24 /30

40.0.1.28 /30

40.0.1.32 /30

40.0.1.36 /30

40.0.1.40 /30

40.0.1.44 /30

40.0.1.48 /30

40.0.2.0 /30

40.0.2.4 /30

40.0.2.8 /30

40.0.2.12 /30

40.0.2.16 /30

40.0.5.0 /24

40.0.7.0 /24

172.10.0.0 /24

172.20.0.0 /24

192.168.1.0 /24

192.168.2.0 /24

-----