This repository has been archived by the owner on May 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
clean_netbox.py
100 lines (75 loc) · 2.57 KB
/
clean_netbox.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
94
95
96
97
98
99
100
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import imp
import sys
import json
import requests
import urllib3
import logging
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# Load config file into variable
conf = imp.load_source('conf', 'conf')
api_url_base = "{}/api".format(conf.NETBOX_HOST)
def api_request(method, url):
# Log which request we're trying to do
logger.debug("HTTP Request: {} - {}".format(method, url))
# Prepare request
request = requests.Request(method, url)
prepared_request = s.prepare_request(request)
response = s.send(prepared_request)
# Log HTTP Response
logger.debug("HTTP Response: {!s} - {}".format(response.status_code, response.reason))
return response
def delete_sites():
logger.info('Deleting Sites')
# Get all sites
api_url = '{}/dcim/sites'.format(api_url_base)
response = api_request('GET', api_url)
sites = json.loads(response.content.decode('utf-8'))
# Delete every site you got
for site in sites['results']:
url = '{}/{}'.format(api_url, site['id'])
response = api_request('DELETE', url)
return
def main():
# We need to delete the items beginning from the most nested items to the top level items
delete_sites()
if __name__ == '__main__':
# Initialize logging platform
logger = logging.getLogger('clean_netbox')
logger.setLevel(logging.DEBUG)
# Log to file
fh = logging.FileHandler(conf.CLEAN_LOG)
fh.setLevel(logging.DEBUG)
# Log to stdout
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# Format log output
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# Attach handlers to logger
logger.addHandler(fh)
logger.addHandler(ch)
# Create HTTP connection pool
s = requests.Session()
# Disable SSL verification
s.verify = False
# Define REST Headers
headers = {'Content-Type': 'application/json',
'Accept': 'application/json; indent=4',
'Authorization': 'Token {0}'.format(conf.NETBOX_TOKEN)}
s.headers.update(headers)
# try:
# import http.client as http_client
# except ImportError:
# # Python 2
# import httplib as http_client
# http_client.HTTPConnection.debuglevel = 1
# requests_log = logging.getLogger("requests.packages.urllib3")
# requests_log.setLevel(logging.DEBUG)
# requests_log.propagate = True
# Run the main function
main()
logger.info('[!] Done!')
sys.exit()