Jan-27-2018, 07:56 PM
Took me all day but got there in the end..
If there is a better way please comment (totally new to this)
If there is a better way please comment (totally new to this)
""" Simple application that logs on to the APIC and displays all of the Endpoints. """ import acitoolkit.acitoolkit as aci import xlwt from datetime import datetime import time def main(): """ Main Show Endpoints Routine :return: None """ # Get timestamp for filename output now = time.time() local_time = time.localtime(now) timestamp = time.strftime("%Y-%m-%d-%H%M", local_time) # Take login credentials from the command line if provided # Otherwise, take them from your environment variables file ~/.profile description = ('Simple application that logs on to the APIC' ' and displays all of the Endpoints.') creds = aci.Credentials('apic', description) args = creds.get() # Login to APIC session = aci.Session(args.url, args.login, args.password) resp = session.login() if not resp.ok: print('%% Could not login to APIC') return # Download all of the interfaces # and store the data as tuples in a list data = [] endpoints = aci.Endpoint.get(session) for ep in endpoints: epg = ep.get_parent() app_profile = epg.get_parent() tenant = app_profile.get_parent() data.append((ep.mac, ep.ip, ep.if_name, ep.encap, tenant.name, app_profile.name, epg.name)) # Write downloaded data to ACI-Endpoints.xls result_excel_file_name = "ACI-Endpoints-" + timestamp + ".xls" headers=["MACADDRESS", "IPADDRESS", "INTERFACE", "ENCAP", "TENANT", "APP PROFILE", "EPG"] wb = xlwt.Workbook(encoding='utf-8') ws = wb.add_sheet('EndPoints') rowx = 0 for colx, value in enumerate(headers): ws.write(rowx, colx, value) ws.set_panes_frozen(True) # frozen headings instead of split panes ws.set_horz_split_pos(rowx + 1) # in general, freeze after last heading row ws.set_remove_splits(True) # if user does unfreeze, don't leave a split there for row in data: rowx += 1 for colx, value in enumerate(row): ws.write(rowx, colx, value.encode('utf-8').decode('utf-8')) wb.save(result_excel_file_name) if __name__ == '__main__': try: main() except KeyboardInterrupt: pass