Skip to content

Commit 4d18095

Browse files
committed
Added APIC-EM gethosts example
1 parent 0aad178 commit 4d18095

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

apic-em_get_hosts/README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Get Hosts List from APIC-EM
2+
3+
This example script uses the requests Python library to interact with the APIC-EM and retrieve the list of clients connected.
4+
5+
This script has been tested with Python 3.5, however may work with other versions.
6+
7+
## DevNet Sandbox
8+
9+
This script targets the Always On APIC-EM DevNet Sandbox.
10+
11+
Find details on the Sandbox [here](https://developer.cisco.com/site/devnet/sandbox/available-labs/networking/).
12+
13+
To execute this script against a different device, update the variables that list the APIC-EM IP, User and Password.
14+
15+
## Requirements
16+
17+
Python
18+
19+
- requests
20+
21+
# Getting Started
22+
23+
* Clone the Python Examples and change into the directory.
24+
25+
```bash
26+
git clone //github.com/CiscoDevNet/python_code_samples_network
27+
cd apic-em_get_hosts
28+
```
29+
30+
* Create and activate a virtualenv
31+
32+
```bash
33+
virtualenv venv --python=python3.5
34+
source venv/bin/activate
35+
```
36+
37+
* Install the requirements
38+
39+
```bash
40+
pip install -r requirements.text
41+
```
42+
43+
* Run the script
44+
45+
```
46+
$ python gethosts.py
47+
Client List from APIC-EM
48+
IP Address MAC Address Type
49+
10.1.15.117 00:24:d7:43:59:d8 wireless
50+
10.2.1.22 5c:f9:dd:52:07:78 wired
51+
10.1.12.20 e8:9a:8f:7a:22:99 wired
52+
```
53+

apic-em_get_hosts/gethosts.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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"]))

apic-em_get_hosts/requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
appdirs==1.4.0
2+
packaging==16.8
3+
pyparsing==2.1.10
4+
requests==2.13.0
5+
six==1.10.0

0 commit comments

Comments
 (0)