├── requirements.txt ├── get_dnac_devices.py ├── env_lab.py ├── README.md └── LICENSE /requirements.txt: -------------------------------------------------------------------------------- 1 | requests==2.18.2 2 | urllib3==1.21.1 3 | prettytable==0.7.2 4 | -------------------------------------------------------------------------------- /get_dnac_devices.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python3 2 | 3 | from env_lab import dnac 4 | import json 5 | import requests 6 | import urllib3 7 | from requests.auth import HTTPBasicAuth 8 | from prettytable import PrettyTable 9 | 10 | 11 | 12 | dnac_devices = PrettyTable(['Hostname','Platform Id','Software Type','Software Version','Up Time' ]) 13 | dnac_devices.padding_width = 1 14 | 15 | # Silence the insecure warning due to SSL Certificate 16 | urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) 17 | 18 | headers = { 19 | 'content-type': "application/json", 20 | 'x-auth-token': "" 21 | } 22 | 23 | 24 | def dnac_login(host, username, password): 25 | url = "https://{}/api/system/v1/auth/token".format(host) 26 | response = requests.request("POST", url, auth=HTTPBasicAuth(username, password), 27 | headers=headers, verify=False) 28 | return response.json()["Token"] 29 | 30 | 31 | def network_device_list(dnac, token): 32 | url = "https://{}/api/v1/network-device".format(dnac['host']) 33 | headers["x-auth-token"] = token 34 | response = requests.get(url, headers=headers, verify=False) 35 | data = response.json() 36 | for item in data['response']: 37 | dnac_devices.add_row([item["hostname"],item["platformId"],item["softwareType"],item["softwareVersion"],item["upTime"]]) 38 | 39 | 40 | login = dnac_login(dnac["host"], dnac["username"], dnac["password"]) 41 | network_device_list(dnac, login) 42 | 43 | print(dnac_devices) 44 | -------------------------------------------------------------------------------- /env_lab.py: -------------------------------------------------------------------------------- 1 | """Set the Environment Information Needed to Access Your Lab! 2 | 3 | The provided sample code in this repository will reference this file to get the 4 | information needed to connect to your lab backend. You provide this info here 5 | once and the scripts in this repository will access it as needed by the lab. 6 | 7 | 8 | Copyright (c) 2018 Cisco and/or its affiliates. 9 | 10 | Permission is hereby granted, free of charge, to any person obtaining a copy 11 | of this software and associated documentation files (the "Software"), to deal 12 | in the Software without restriction, including without limitation the rights 13 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 | copies of the Software, and to permit persons to whom the Software is 15 | furnished to do so, subject to the following conditions: 16 | 17 | The above copyright notice and this permission notice shall be included in all 18 | copies or substantial portions of the Software. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | SOFTWARE. 27 | """ 28 | 29 | 30 | # User Input 31 | 32 | # Please select the lab environment that you will be using today 33 | # sandbox - Cisco DevNet Always-On / Reserved Sandboxes 34 | # express - Cisco DevNet Express Lab Backend 35 | # custom - Your Own "Custom" Lab Backend 36 | ENVIRONMENT_IN_USE = "sandbox" 37 | 38 | # Set the 'Environment Variables' based on the lab environment in use 39 | if ENVIRONMENT_IN_USE == "sandbox": 40 | dnac = { 41 | "host": "sandboxdnac.cisco.com", 42 | "port": 443, 43 | "username": "devnetuser", 44 | "password": "Cisco123!" 45 | } 46 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Getting started 2 | 3 | These instructions will get you a copy of the Python code for a Python script to gather information of of the network devices a DNAC controller knows about and their attributes. 4 | 5 | ## Requirements 6 | 7 | - Python 3.6 or higher 8 | - "git" command line tools 9 | - Homebrew (Mac OS X) 10 | 11 | ## For Mac OS X Installation 12 | 13 | ``` 14 | git installation - https://git-scm.com/download/mac 15 | ``` 16 | ``` 17 | homebrew installation - ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)" 18 | ``` 19 | ``` 20 | Python 3.6 installation - https://www.python.org/downloads/release/python-364/ 21 | Python pip installation 22 | curl -o get-pip.py https://bootstrap.pypa.io/get-pip.py 23 | sudo python get-pip.py 24 | ``` 25 | Command Line Developer Tools Installation. After running command, complete installation using the GUI. 26 | ``` 27 | xcode-select --install 28 | ``` 29 | 30 | ## For Windows Installation 31 | ``` 32 | git installation - https://git-scm.com/download/win 33 | Python 3.6 installation - https://www.python.org/downloads/release/python-364/ 34 | Be sure to check box for "Add Python to PATH" during the installer 35 | ``` 36 | 37 | ## 'GIT' this code 38 | 39 | All of the code and examples for this lesson is located in the 'add me here' directory. Clone and access it with the following commands: 40 | 41 | ``` 42 | git clone https://github.com/bigevilbeard/dnac-device-info 43 | cd dnac-device-info 44 | ``` 45 | Use pip to install the necessary requirements 46 | ``` 47 | pip install -r requirements.txt 48 | ``` 49 | 50 | ### Code features 51 | 52 | The Python script uses the DNCA APIs to get device information. The APIs provides a list of all of the network devices the DNCA controller knows about and all of their attributes. For example hostname, serial platform type, software version, uptime etc. You can either get all of the devices, or a subset. This is printed out using PrettyTable, this is a simple Python library designed to make it quick and easy to represent tabular data in visually appealing ASCII tables. It was inspired by the ASCII tables used in the PostgreSQL shell psql. PrettyTable allows for selection of which columns are to be printed, independent alignment of columns (left or right justified or centred) and printing of “sub-tables” by specifying a row range. 53 | 54 | ## DNAC API reference 55 | 56 | If you look at the Python code for our script, you will see the API calls used. 57 | 58 | - `https://{}/api/system/v1/auth/token` Gets and encapsulates user identity and role information as a single value that RBAC-governed APIs use to make access-control decisions. This returns a token as a response in a JSON object. You can re-use it for any request that may need it directly (such as the Python script here) 59 | - `https://{}/api/v1/network-device` Gets the list of first 500 network devices sorted lexicographically based on hostname. It can be filtered using management IP address, mac address, hostname and location name. If id param is provided, it will be returning the list of network-devices for the given id's and other request params will be ignored. In case of autocomplete request, returns the list of specified attributes. 60 | 61 | ## Running the code 62 | ``` 63 | python get_dnac_devices.py 64 | +-------------------+----------------+---------------+------------------+-----------------------+ 65 | | Hostname | Platform Id | Software Type | Software Version | Up Time | 66 | +-------------------+----------------+---------------+------------------+-----------------------+ 67 | | asr1001-x.abc.inc | ASR1001-X | IOS-XE | 16.6.1 | 168 days, 18:22:47.35 | 68 | | cat_9k_1.abc.inc | C9300-24UX | IOS-XE | 16.6.1 | 168 days, 19:31:10.69 | 69 | | cat_9k_2.abc.inc | C9300-24UX | IOS-XE | 16.6.1 | 168 days, 19:07:32.50 | 70 | | cs3850.abc.inc | WS-C3850-48U-E | IOS-XE | 16.6.2s | 165 days, 6:07:48.75 | 71 | +-------------------+----------------+---------------+------------------+-----------------------+ 72 | ``` 73 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | CISCO SAMPLE CODE LICENSE 2 | Version 1.0 3 | Copyright (c) 2017 Cisco and/or its affiliates 4 | 5 | These terms govern this Cisco example or demo source code and its 6 | associated documentation (together, the "Sample Code"). By downloading, 7 | copying, modifying, compiling, or redistributing the Sample Code, you 8 | accept and agree to be bound by the following terms and conditions (the 9 | "License"). If you are accepting the License on behalf of an entity, you 10 | represent that you have the authority to do so (either you or the entity, 11 | "you"). Sample Code is not supported by Cisco TAC and is not tested for 12 | quality or performance. This is your only license to the Sample Code and 13 | all rights not expressly granted are reserved. 14 | 15 | 1. LICENSE GRANT: Subject to the terms and conditions of this License, 16 | Cisco hereby grants to you a perpetual, worldwide, non-exclusive, non- 17 | transferable, non-sublicensable, royalty-free license to copy and 18 | modify the Sample Code in source code form, and compile and 19 | redistribute the Sample Code in binary/object code or other executable 20 | forms, in whole or in part, solely for use with Cisco products and 21 | services. For interpreted languages like Java and Python, the 22 | executable form of the software may include source code and 23 | compilation is not required. 24 | 25 | 2. CONDITIONS: You shall not use the Sample Code independent of, or to 26 | replicate or compete with, a Cisco product or service. Cisco products 27 | and services are licensed under their own separate terms and you shall 28 | not use the Sample Code in any way that violates or is inconsistent 29 | with those terms (for more information, please visit: 30 | www.cisco.com/go/terms. 31 | 32 | 3. OWNERSHIP: Cisco retains sole and exclusive ownership of the Sample 33 | Code, including all intellectual property rights therein, except with 34 | respect to any third-party material that may be used in or by the 35 | Sample Code. Any such third-party material is licensed under its own 36 | separate terms (such as an open source license) and all use must be in 37 | full accordance with the applicable license. This License does not 38 | grant you permission to use any trade names, trademarks, service 39 | marks, or product names of Cisco. If you provide any feedback to Cisco 40 | regarding the Sample Code, you agree that Cisco, its partners, and its 41 | customers shall be free to use and incorporate such feedback into the 42 | Sample Code, and Cisco products and services, for any purpose, and 43 | without restriction, payment, or additional consideration of any kind. 44 | If you initiate or participate in any litigation against Cisco, its 45 | partners, or its customers (including cross-claims and counter-claims) 46 | alleging that the Sample Code and/or its use infringe any patent, 47 | copyright, or other intellectual property right, then all rights 48 | granted to you under this License shall terminate immediately without 49 | notice. 50 | 51 | 4. LIMITATION OF LIABILITY: CISCO SHALL HAVE NO LIABILITY IN CONNECTION 52 | WITH OR RELATING TO THIS LICENSE OR USE OF THE SAMPLE CODE, FOR 53 | DAMAGES OF ANY KIND, INCLUDING BUT NOT LIMITED TO DIRECT, INCIDENTAL, 54 | AND CONSEQUENTIAL DAMAGES, OR FOR ANY LOSS OF USE, DATA, INFORMATION, 55 | PROFITS, BUSINESS, OR GOODWILL, HOWEVER CAUSED, EVEN IF ADVISED OF THE 56 | POSSIBILITY OF SUCH DAMAGES. 57 | 58 | 5. DISCLAIMER OF WARRANTY: SAMPLE CODE IS INTENDED FOR EXAMPLE PURPOSES 59 | ONLY AND IS PROVIDED BY CISCO "AS IS" WITH ALL FAULTS AND WITHOUT 60 | WARRANTY OR SUPPORT OF ANY KIND. TO THE MAXIMUM EXTENT PERMITTED BY 61 | LAW, ALL EXPRESS AND IMPLIED CONDITIONS, REPRESENTATIONS, AND 62 | WARRANTIES INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTY OR 63 | CONDITION OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON- 64 | INFRINGEMENT, SATISFACTORY QUALITY, NON-INTERFERENCE, AND ACCURACY, 65 | ARE HEREBY EXCLUDED AND EXPRESSLY DISCLAIMED BY CISCO. CISCO DOES NOT 66 | WARRANT THAT THE SAMPLE CODE IS SUITABLE FOR PRODUCTION OR COMMERCIAL 67 | USE, WILL OPERATE PROPERLY, IS ACCURATE OR COMPLETE, OR IS WITHOUT 68 | ERROR OR DEFECT. 69 | 70 | 6. GENERAL: This License shall be governed by and interpreted in 71 | accordance with the laws of the State of California, excluding its 72 | conflict of laws provisions. You agree to comply with all applicable 73 | United States export laws, rules, and regulations. If any provision of 74 | this License is judged illegal, invalid, or otherwise unenforceable, 75 | that provision shall be severed and the rest of the License shall 76 | remain in full force and effect. No failure by Cisco to enforce any of 77 | its rights related to the Sample Code or to a breach of this License 78 | in a particular situation will act as a waiver of such rights. In the 79 | event of any inconsistencies with any other terms, this License shall 80 | take precedence. 81 | --------------------------------------------------------------------------------