-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecurity-API-demo.py
71 lines (59 loc) · 2.56 KB
/
security-API-demo.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
#
# Generated FMC REST API sample script
#
import json
import sys
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
server = "https://fmcrestapisandbox.cisco.com"
username = "joeljos"
password = "NKQXPNBc"
r = None
headers = {'Content-Type': 'application/json'}
api_auth_path = "/api/fmc_platform/v1/auth/generatetoken"
auth_url = server + api_auth_path
try:
# 2 ways of making a REST call are provided:
# One with "SSL verification turned off" and the other with "SSL verification turned on".
# The one with "SSL verification turned off" is commented out. If you like to use that then
# uncomment the line where verify=False and comment the line with =verify='/path/to/ssl_certificate'
# REST call with SSL verification turned off:
# r = requests.post(auth_url, headers=headers, auth=requests.auth.HTTPBasicAuth(username,password), verify=False)
# REST call with SSL verification turned on: Download SSL certificates from your FMC first and provide its path for verification.
r = requests.post(auth_url, headers=headers, auth=requests.auth.HTTPBasicAuth(username,password), verify=False)
auth_headers = r.headers
auth_token = auth_headers.get('X-auth-access-token', default=None)
if auth_token == None:
print("auth_token not found. Exiting...")
sys.exit()
else:
print("token is ",auth_token)
except Exception as err:
print ("Error in generating auth token --> "+str(err))
sys.exit()
headers['X-auth-access-token']=auth_token
#api_path = "/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/object/networkaddresses" # param
api_path = "/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/object/countries"
url = server + api_path
if (url[-1] == '/'):
url = url[:-1]
# GET OPERATION
try:
# REST call with SSL verification turned off:
# r = requests.get(url, headers=headers, verify=False)
# REST call with SSL verification turned on:
r = requests.get(url, headers=headers, verify=False)
status_code = r.status_code
resp = r.text
if (status_code == 200):
print("GET successful. Response data --> ")
json_resp = json.loads(resp)
print(json.dumps(json_resp,sort_keys=True,indent=4, separators=(',', ': ')))
else:
r.raise_for_status()
print("Error occurred in GET --> "+resp)
except requests.exceptions.HTTPError as err:
print ("Error in connection --> "+str(err))
finally:
if r : r.close()