Skip to content

Instantly share code, notes, and snippets.

@kecorbin
Created October 9, 2018 21:29
Show Gist options
  • Save kecorbin/b8ff89ae8e6c05919b9e45e3fc936dd9 to your computer and use it in GitHub Desktop.
Save kecorbin/b8ff89ae8e6c05919b9e45e3fc936dd9 to your computer and use it in GitHub Desktop.
Determine if a port is in use on ACI
#!/usr/bin/env python
"""
Find out where a DN is used
"""
from acitoolkit import Credentials, Session
from tabulate import tabulate
data = []
def main():
"""
Main execution routine
"""
description = ('Simple application that logs on to the APIC'
' and displays usage information for a given DN')
creds = Credentials('apic', description)
creds.add_argument("-port", "--port",
help="port in node/mod/port format e.g 101/1/1")
args = creds.get()
node, mod, port = args.port.split('/')
session = Session(args.url, args.login, args.password)
resp = session.login()
if not resp.ok:
print('%% Could not login to APIC')
url = "/api/node/mo/topology/pod-1/node-{}/sys/phys-[eth{}/{}].json"
url = url.format(node, mod, port)
query_options = "?rsp-subtree-include=full-deployment" \
"&target-node=all" \
"&target-path=l1EthIfToEPg"
url = url + query_options
resp = session.get(url)
if resp.ok:
port = resp.json()['imdata'][0]['l1PhysIf']
children = port.get('children')
if not children:
print("All clear!")
exit()
for item in children:
try:
epg = item['pconsCtrlrDeployCtx']['children'][0]['pconsResourceCtx']['attributes']['ctxDn']
data.append([epg])
except:
print("rur roh")
exit()
print(tabulate(data, headers=["Used by", "Class"]))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment