2.3.2.1 Lab - Create A Host Inventory in Python
2.3.2.1 Lab - Create A Host Inventory in Python
2.3.2.1 Lab - Create A Host Inventory in Python
Objectives
Part 1: Using Postman to Get a Network Host Inventory
Part 2: Using Python to Get a Network Host Inventory
Background / Scenario
In a production network, the hosts on the network are constantly changing. From a security point of view, it is
very useful to know about these hosts and where they connect. The APIC-EM keeps track of the hosts that
are connected on the network. This information can be periodically collected and processed by Python. In this
lab, you will create a function that displays an inventory of hosts that can be processed by other Python
programs.
First, you will learn about the requirements for making the host inventory request by using Postman. You will
review the structure of the JSON that comprises the inventory. Then, you will replicate the process in a
Python program.
Required Resources
• Postman
• Python 3 with IDLE
• Python requests module
• Python tabulate module
• The functions file that you have created or the apic_em_functions_sol.py file
• Access to the Internet
Note: Use the APIC-EM sandbox URL and credentials provided by your instructor. Only use the public
APIC-EM URL and credentials for additional study after the conclusion of the workshop. For example
purposes, this activity uses the URL and credentials of the public sandbox.
Step 1: Configure and send the Postman request for a host inventory.
a. Click the plus sign (+) to create a new tab.
b. Enter the following information:
1) Request method: GET
2) Endpoint URI: https://{YOUR-APICEM}.cisco.com/api/v1/host
3) Headers:
Content-Type: application/json
Cisco and/or its affiliates. All rights reserved. Cisco Confidential Page 1 of 8 www.netacad.com
Lab - Create a Host Inventory in Python
Cisco and/or its affiliates. All rights reserved. Cisco Confidential Page 2 of 8 www.netacad.com
Lab - Create a Host Inventory in Python
Cisco and/or its affiliates. All rights reserved. Cisco Confidential Page 3 of 8 www.netacad.com
Lab - Create a Host Inventory in Python
c. Collapse all levels by clicking the second icon in the Result window, as shown above. Expand object,
array, and response. The number next to the response key indicates how many entries there are.
Expand the level marked 0. Your Tree Viewer should look similar to the following:
Cisco and/or its affiliates. All rights reserved. Cisco Confidential Page 4 of 8 www.netacad.com
Lab - Create a Host Inventory in Python
Next to level 0 in the example, the number 14 indicates that there are 14 keys associated with this entry.
However, there are different numbers for the other hosts. Open each device and compare entries. Why is
there a difference?
____________________________________________________________________________________
____________________________________________________________________________________
Cisco and/or its affiliates. All rights reserved. Cisco Confidential Page 5 of 8 www.netacad.com
Lab - Create a Host Inventory in Python
b. Next, print the status of the request to display the status of the request. Because resp is a requests
object, it has a property called status_code. Print a string with some explanatory text followed by the
status code.
print("Status of /host request: ", resp.status_code)
c. If the API response status code of the request is different from code 200 (that indicates OK), the
application should stop the execution because most probably an error has occurred in the API call. Verify
with a simple if statement if the value of the resp.status_code equals 200. If not, raise an exception to
stop the execution:
if resp.status_code != 200:
raise Exception("Status code does not equal 200. Response text: " + resp.text)
This creates a custom error message that can help to identify the root cause of the error. As shown
below.
This output indicates there is an authorization problem possibly because the service ticket is invalid or
there is a problem with the get_ticket() function.
d. Convert the JSON response data into a Python dictionary format with the following statement:
response_json = resp.json()
The entire block of code should look like this:
resp = requests.get(api_url, headers=headers, verify=False)
print("Status of /host request: ", resp.status_code)
if resp.status_code != 200:
raise Exception("Status code does not equal 200. Response text: " + resp.text)
response_json = resp.json()
e. Save your script and run it. You should get output similar to the following. If not, troubleshoot your code
for errors.
The service ticket number is: ST-5873-WBA3XaHjev0aJYeWPVk3-cas
Status of /host request: 200
>>>
Cisco and/or its affiliates. All rights reserved. Cisco Confidential Page 6 of 8 www.netacad.com
Lab - Create a Host Inventory in Python
To do this, a for: loop is created. The for: loop will iterate through the list of hosts and extract the value for
the two desired dictionary keys. They are the hostType and hostIP keys of the response dictionary. You will
create a list variable called host_list that will contain the values for the Number, Type, and IP columns. Each
host will be appended to the list as a separate line as the loop executes. Execution of loop stops when there
are no more items under the response dictionary key.
The number of the host is not present in the current JSON data. You will create a separate variable, assign a
value of 0 and then increment it as the loop repeats.
a. First initialize the two required variables. The first is a list variable called host_list. The second is an
integer variable that will hold the value for the Number column in the table.
host_list = []
i = 0
b. Now create the for: loop. The loop will iterate over every item in the response key of the response_json
variable. Each time that loop iterates, the item variable takes on the value of the JSON for that item in
response, which corresponds to each host. The statement that does this is:
for item in response_json["response"]:
c. The next lines of code will execute as the for: loop runs. They should be indented. First, the ordinal
number variable i is incremented through each loop:
i += 1
Then, append the values from the JSON to the host_list variable. The key to understanding this line of
code is that the variable item takes on the value of the keys under the response key with every iteration.
In essence, item holds all the information in the JSON for device 0 in the first iteration, device 1 in the
next, etc. This means that item has the hostType and hostIP keys within it. The code appends the value
for the ordinal variable i, the value for hostType, and the value for hostIP to the host_list variable. As
the loop iterates it does this for each host.
host = [ i, item["hostType"], item["hostIp"] ]
host_list.append( host )
The complete for: loop should look like this:
for item in response_json["response"]:
i+=1
host = [
i,
item["hostType"],
item["hostIp"]
]
host_list.append( host )
d. Finally, use the tabulate function to print the table of hosts. The tabulate function will take as arguments
the host_list variable, a list of headers for the columns that will be printed, and a setting for the table
format.
table_header = ["Number", "Type", "IP"]
print( tabulate(host_list, table_header) )
e. Save your script and run it. You should get a table similar to the one shown at the beginning of this step.
Investigate any errors that may occur. Look for mismatched paired symbols like (), [], and " ".
Cisco and/or its affiliates. All rights reserved. Cisco Confidential Page 7 of 8 www.netacad.com
Lab - Create a Host Inventory in Python
a. The my_apic_em_functions.py should already have the import statements for requests and json. Copy
and paste the tabulate import statement into the file below them.
b. Add a few blank lines below the get_ticket() function and define a new function called print_hosts():.
Copy your code from the api_url variable through the print statement and paste it below print_hosts():.
c. Select all of the lines below the function definition statement and select Indent Region from the IDLE
format menu, or press the TAB key. Everything should be indented an additional four spaces. This
function does not require a return statement. Your functions file should look like the following. Make sure
your indention is correct for each line of code:
d. Save and run the functions file. In the IDLE Shell, type print hosts() at the prompt. The function should
run just as your program did.
Cisco and/or its affiliates. All rights reserved. Cisco Confidential Page 8 of 8 www.netacad.com