-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathise-ers-count.py
executable file
·73 lines (56 loc) · 1.96 KB
/
ise-ers-count.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python3
"""
Get the total number of a specific ISE ERS resource.
See https://cs.co/ise-api for REST API resource names.
Usage: ise-ers-count.py {resource_name}
Requires setting the these environment variables using the `export` command:
export ISE_PPAN='1.2.3.4' # hostname or IP address of ISE Primary PAN
export ISE_REST_USERNAME='admin' # ISE ERS admin or operator username
export ISE_REST_PASSWORD='C1sco12345' # ISE ERS admin or operator password
export ISE_CERT_VERIFY=false # validate the ISE certificate
You may add these export lines to a text file and load with `source`:
source ise.sh
"""
__author__ = "Thomas Howard"
__email__ = "thomas@cisco.com"
__license__ = "MIT - https://mit-license.org/"
import requests
import os
import sys
# Silence any warnings about certificates
requests.packages.urllib3.disable_warnings()
"""
Return the number of resources of type resource.
"""
def ise_ers_resource_count(resource):
count = 0
r = requests.get(
f"https://{ENV['ISE_PPAN']}/ers/config/{resource_name}",
auth=(ENV["ISE_REST_USERNAME"], ENV["ISE_REST_PASSWORD"]),
headers={"Accept": "application/json"},
verify=ENV["ISE_CERT_VERIFY"].lower().startswith("t"),
)
if r.status_code == 200:
count = r.json()["SearchResult"]["total"]
elif r.status_code == 404:
print(f"{r.status_code} Unknown resource: {resource}", file=sys.stderr)
else:
print(f"{r.status_code} uh oh {r.text}", file=sys.stderr)
return count
"""
__main__
"""
if __name__ == "__main__":
"""
Run from script
"""
# Load Environment Variables
ENV = {k: v for (k, v) in os.environ.items()}
if len(sys.argv) <= 1:
print("❌ Missing resource name", file=sys.stderr)
print(USAGE, file=sys.stderr)
sys.exit(1) # not OK
resource_name = sys.argv[1]
count = ise_ers_resource_count(resource_name)
print(count)
sys.exit(0) # 0 == OK