|
| 1 | + |
| 2 | +""" |
| 3 | +Simple application that logs on to the APIC-EM and displays all |
| 4 | +of the clients connected. |
| 5 | +
|
| 6 | +Leverages the DevNet Sandbox - APIC-EM Always On |
| 7 | + Information at https://developer.cisco.com/site/devnet/sandbox/available-labs/networking/ |
| 8 | + |
| 9 | +""" |
| 10 | + |
| 11 | + |
| 12 | +# Import necessary modules |
| 13 | +import requests |
| 14 | + |
| 15 | +# Disable warnings |
| 16 | +requests.packages.urllib3.disable_warnings() |
| 17 | + |
| 18 | +# Variables |
| 19 | +apic_em_ip = "https://sandboxapic.cisco.com/api/v1" |
| 20 | +apic_em_user = "devnetuser" |
| 21 | +apic_em_password = "Cisco123!" |
| 22 | + |
| 23 | +def get_token(url): |
| 24 | + |
| 25 | + # Define API Call |
| 26 | + api_call = "/ticket" |
| 27 | + |
| 28 | + # Payload contains authentication information |
| 29 | + payload = {"username": apic_em_user, "password": apic_em_password} |
| 30 | + |
| 31 | + # Header information |
| 32 | + headers = {"content-type": "application/json"} |
| 33 | + |
| 34 | + # Combine URL, API call and parameters variables |
| 35 | + url += api_call |
| 36 | + |
| 37 | + response = requests.post(url, json=payload, headers=headers, verify=False).json() |
| 38 | + |
| 39 | + # Return authentication token from respond body |
| 40 | + return response["response"]["serviceTicket"] |
| 41 | + |
| 42 | +def get_hosts(token, url): |
| 43 | + # Define API Call |
| 44 | + api_call = "/host" |
| 45 | + |
| 46 | + # Header information |
| 47 | + headers = {"X-AUTH-TOKEN": token} |
| 48 | + |
| 49 | + # Combine URL, API call and parameters variables |
| 50 | + url += api_call |
| 51 | + |
| 52 | + response = requests.get(url, headers=headers, verify=False).json() |
| 53 | + |
| 54 | + # Get hosts list from response and return |
| 55 | + hosts = response["response"] |
| 56 | + return hosts |
| 57 | + |
| 58 | + |
| 59 | +# Assign obtained authentication token to a variable. Provide APIC-EM's |
| 60 | +# URL address |
| 61 | +auth_token = get_token(apic_em_ip) |
| 62 | + |
| 63 | +# Get list of hosts |
| 64 | +hosts = get_hosts(auth_token, apic_em_ip) |
| 65 | + |
| 66 | +# Display in table |
| 67 | +print("Client List from APIC-EM") |
| 68 | +print("{ip:20} {mac:20} {type:10}".format(ip="IP Address", |
| 69 | + mac="MAC Address", |
| 70 | + type="Type")) |
| 71 | + |
| 72 | +for host in hosts: |
| 73 | + print("{ip:20} {mac:20} {type:10}".format(ip=host["hostIp"], |
| 74 | + mac=host["hostMac"], |
| 75 | + type=host["hostType"])) |
0 commit comments