-
Notifications
You must be signed in to change notification settings - Fork 3
/
delete-hosts-from-csv-070620.py
93 lines (82 loc) · 3.04 KB
/
delete-hosts-from-csv-070620.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import csv
import json
import sys
import requests
import os
import getpass
import urllib3
#Prevent SSL security warning
urllib3.disable_warnings()
#Get FMC Server
server_start = "https://"
server_main = input("\nEnter the IP or FQDN of your FMC: https://")
server = server_start + server_main
#Get FMC Credentials
username = input("Username: ")
password = getpass.getpass("Password: ")
#Update to console
print("\nAccessing the FMC API, please wait...", end="")
#Define authentication elements
headers = {'Content-Type': 'application/json'}
api_auth_path = "/api/fmc_platform/v1/auth/generatetoken"
auth_url = server + api_auth_path
r = None
#Generate insecure auth token for API operation
try:
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)
auth_domain = auth_headers.get('DOMAIN_UUID', default=None)
if auth_token == None:
print("auth_token not found. Exiting...")
sys.exit()
except Exception as err:
print("Error generating auth token -> "+str(err))
sys.exit()
#Create authenticated url for API operation
headers['X-auth-access-token']=auth_token
print('Connected, auth token collected (' + auth_token + (')\n'))
api_path = "/api/fmc_config/v1/domain/" + auth_domain + "/object/hosts/"
#CSV operation and file : csv file called in script command
#csvfile = open(sys.argv[1])
#objects = csv.DictReader(csvfile)
#CSV operation and file specified
f = open("del-host-by-id.csv")
elementsfile = csv.DictReader(f)
#Identify data to use for host deletion
for element in elementsfile:
del_data = {
element["objectid"],
}
#Create authenticated url for API operation
for element in del_data:
del_url = server + api_path + element
try:
#Update to console
print(del_url)
r = requests.delete(del_url, headers=headers, verify=False)
status_code = r.status_code
resp = r.text
log = open('delete_objects.log', 'a')
print(" Status code: "+str(status_code))
json_resp = json.loads(resp)
log.write('\n=====\n')
log.write(json.dumps(json_resp,sort_keys=True,indent=4, separators=(',', ': ')))
#Notify codes to console
if status_code == 201 or status_code == 202:
print(" SUCCESS ")
elif status_code == 400:
print((" Message: ")+ resp + ('\n'))
elif status_code == 404:
print((" Message: ")+ resp + ('\n'))
else:
r.raise_for_status()
print((" Message: ")+ resp + ('\n'))
except requests.exceptions.HTTPError as err:
print("Connection error -> "+str(err))
finally:
if r: r.close()
#Update to console
print('\nLog file "delete_objects.log" appended\n')
#End script mode
input("Press <Enter> to return to CMD prompt")