-
Notifications
You must be signed in to change notification settings - Fork 2
/
ise-post-ers-from-file.py
executable file
·61 lines (48 loc) · 2 KB
/
ise-post-ers-from-file.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
#!/usr/bin/env python3
"""
A simple POST request for an ISE ERS resource.
See https://cs.co/ise-api for REST API resource names.
Usage:
ise-post-ers-from_file.py {resource_name} {resource_file.json}
ise-post-ers-from_file.py networkdevice my_network_device.json
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-env.sh
"""
__author__ = "Thomas Howard"
__email__ = "thomas@cisco.com"
__license__ = "MIT - https://mit-license.org/"
import requests
import json
import os
import sys
requests.packages.urllib3.disable_warnings() # Silence any warnings about certificates
# Validate command line arguments
if len(sys.argv) < 3 :
print(__doc__)
sys.exit(1)
resource_name = sys.argv[1]
json_filepath = sys.argv[2]
# Load the JSON data
json_data = ''
with open(json_filepath) as f: json_data = f.read()
print(json_data)
env = {k:v for (k,v) in os.environ.items() } # Load Environment Variables
# POST the resource
url = f"https://{env['ISE_PPAN']}/ers/config/{resource_name}"
basic_auth = (env['ISE_REST_USERNAME'], env['ISE_REST_PASSWORD'])
json_headers = { 'Accept': 'application/json', 'Content-Type': 'application/json' }
ssl_verify = False if env['ISE_CERT_VERIFY'][0].lower() in ['f','n'] else True
r = requests.post(url, auth=basic_auth, headers=json_headers, data=json_data, verify=ssl_verify)
print(r.status_code)
if r.status_code == 201 :
print(f'✅ View your new {resource_name}\n {r.headers["Location"]}')
elif r.status_code == 401 :
print(f'X {r.status_code}\n {json.dumps(r.json(), indent=2)}')
print(USAGE, file=sys.stderr)
else :
print(json.dumps(r.json(), indent=2))