-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdnac-delete-discoveries.py
69 lines (53 loc) · 2.13 KB
/
dnac-delete-discoveries.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
# This script will delete all existing discovery jobs from DNA Center
# this can be useful if many jobs have built up over time and need to be cleaned up
# BASE_URL variable needs to be updated with the IP address of your DNAC appliance, or in the case of a cluster, the DNAC VIP
# developed using Python 3.6.8
import requests
from requests.auth import HTTPBasicAuth
# Disable SSL warnings
import urllib3
urllib3.disable_warnings()
# Variables
BASE_URL = 'https://xxx.xxx.xxx.xxx'
AUTH_URL = '/dna/system/api/v1/auth/token'
def get_token():
print('\n\nEnter DNA Center Credentials')
user = input("USERNAME: ").strip()
passwd = input("PASSWORD: ").strip()
response = requests.post(
BASE_URL + AUTH_URL,
auth=HTTPBasicAuth(username=user, password=passwd),
headers={'content-type': 'application/json'},
verify=False,
)
data = response.json()
if response:
return data['Token']
else:
sys.exit('Unable to connect to ' + BASE_URL + ' using supplied credentials')
def delete_discovery_jobs(token):
try:
response = requests.delete(
BASE_URL + '/dna/intent/api/v1/discovery',
headers = {'X-Auth-Token': token, 'Content-Type': 'application/json'},
verify=False,
)
except requests.exceptions.RequestException as e:
return e
return response
def main():
#get DNAC authorization token to be used with all API calls
token = get_token()
delete_jobs = input('\nDo you want to delete all existing discovery jobs? (y/n)')
inputvalid = (delete_jobs.lower() == 'y' or delete_jobs.lower() == 'n')
while inputvalid != True:
delete_jobs = input('Do you want to delete devices prior to redicovery? (y/n)')
inputvalid = (delete_jobs.lower() == 'y' or delete_jobs.lower() == 'n')
if delete_jobs.lower() == 'y':
result = delete_discovery_jobs(token)
# print(result)
print('\nAll discovery jobs completed')
else:
print('\nProgram aborted!')
if __name__ == "__main__":
main()