-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcisco_api_lab.py
More file actions
57 lines (43 loc) · 1.78 KB
/
cisco_api_lab.py
File metadata and controls
57 lines (43 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import requests
# Cisco DevNet Sandbox Details
BASE_URL = "https://sandboxdnac.cisco.com"
USERNAME = "devnetuser"
PASSWORD = "Cisco123!"
# Disable SSL warnings
requests.packages.urllib3.disable_warnings()
def get_auth_token():
"""Obtain authentication token from Cisco DNA Center API."""
url = f"{BASE_URL}/dna/system/api/v1/auth/token"
response = requests.post(url, auth=(USERNAME, PASSWORD), verify=False)
if response.status_code == 200:
token = response.json()["Token"]
print("✅ Authentication Successful!")
return token
else:
print(f"❌ Authentication Failed: {response.status_code} - {response.text}")
return None
def get_devices(token):
"""Fetch network devices from Cisco DNA Center API."""
url = f"{BASE_URL}/dna/intent/api/v1/network-device"
headers = {"X-Auth-Token": token, "Content-Type": "application/json"}
response = requests.get(url, headers=headers, verify=False)
if response.status_code == 200:
devices = response.json().get("response", [])
if not devices:
print("❌ No devices found.")
return
print("\n📌 Network Devices:")
for device in devices:
hostname = device.get("hostname", "Unknown")
mgmt_ip = device.get("managementIpAddress", "Unknown")
device_type = device.get("type", device.get("platformId", "Unknown"))
print(f"🔹 Hostname: {hostname}")
print(f"🌐 Management IP: {mgmt_ip}")
print(f"🔧 Device Type: {device_type}")
print("-" * 50)
else:
print(f"❌ Failed to retrieve devices: {response.status_code} - {response.text}")
# Run authentication and fetch devices
token = get_auth_token()
if token:
get_devices(token)