-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgetExporters.py
executable file
·214 lines (161 loc) · 5.56 KB
/
getExporters.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#####################
# ABOUT THIS SCRIPT #
#####################
#
# getExporters.py
# ----------------
# Author: Alan Nix
# Property of: Cisco Systems
# Version: 2.0
# Release Date: 01/02/2018
#
# Summary
# -------
#
# This script exports the "Exporters" out of StealthWatch into a CSV file
#
#
# Requirements
# ------------
#
# 1) Must have Python 3.x installed.
# 2) Must have 'requests' Python module installed. Easiest way to do that:
# - wget https://bootstrap.pypa.io/get-pip.py
# - python get-pip.py (may need to use 'sudo')
# - pip install requests (may need to use 'sudo')
#
# How To Run
# ----------
#
# 1) Optionally configure StealthWatch SW_SMC_ADDRESS, SW_USERNAME, SW_PASSWORD
# 2) Run the script
#
############################################################
import csv
import getpass
import json
import requests
from requests.auth import HTTPBasicAuth
from requests.packages import urllib3
# Disable SSL Cert warnings
urllib3.disable_warnings()
####################
# CONFIGURATION #
####################
#
# ---------------------------------------------------- #
#
# Setup an API session
API_SESSION = requests.Session()
# StealthWatch SMC Variables
SW_DOMAIN_ID = None
SW_SMC_ADDRESS = ""
SW_USERNAME = ""
SW_PASSWORD = ""
#
# ---------------------------------------------------- #
####################
# FUNCTIONS #
####################
def getAccessToken():
'''Get REST API Token'''
# The URL to authenticate to the SMC
url = "https://" + SW_SMC_ADDRESS + "/token/v2/authenticate"
print('Stealthwatch login URL: ' + url)
# JSON to hold the authentication credentials
login_credentials = {
"username": SW_USERNAME,
"password": SW_PASSWORD
}
try:
# Make an authentication request to the SMC
response = API_SESSION.post(url, data=login_credentials, verify=False)
# If the request was successful, then proceed
if response.status_code == 200:
print('Successfully Authenticated.')
return response.text
else:
print('SMC Connection Failure - HTTP Return Code: {}\nResponse: {}'.format(response.status_code, response.text))
exit()
except Exception as err:
print('Unable to post to the SMC - Error: {}'.format(err))
exit()
def getTenants():
'''Get the "tenants" (domains) from Stealthwatch'''
global SW_DOMAIN_ID
# The URL to get tenants
url = 'https://' + SW_SMC_ADDRESS + '/sw-reporting/v1/tenants/'
# Get the tenants
response = API_SESSION.get(url, verify=False)
# If the request was successful, then proceed
if response.status_code == 200:
# Parse the response as JSON
tenants = json.loads(response.text)
tenants = tenants['data']
# Set the Domain ID if theres only one, or prompt the user if there are multiple
if len(tenants) == 1:
SW_DOMAIN_ID = tenants[0]['id']
else:
print("\nPlease select one of the following Domains:\n")
domain_index = 1
# Print the domain options that are available
for tenant in tenants:
print("{}) {}".format(domain_index, tenant['displayName']))
domain_index += 1
# Prompt the user for the Domain
selected_domain = input("\nDomain Selection: ")
# Make sure that the selected domain was valid
if 0 < int(selected_domain) <= len(tenants):
selected_domain = int(selected_domain) - 1
else:
print("ERROR: Domain selection was not correct.")
exit()
SW_DOMAIN_ID = tenants[selected_domain]['id']
else:
print('SMC Connection Failure - HTTP Return Code: {}\nResponse: {}'.format(response.status_code, response.text))
exit()
def getExporters():
''' Get the Exporters from Stealthwatch '''
# The URL to get Exporters
url = 'https://{}/sw-reporting/v1/tenants/{}/netops/exporters/details/True'.format(SW_SMC_ADDRESS, SW_DOMAIN_ID)
# Get the Exporters
response = API_SESSION.get(url, verify=False)
# If the request was successful, then proceed
if response.status_code == 200:
# Parse the response as JSON
exporters = json.loads(response.text)
exporters = exporters['data']
return exporters
else:
print('SMC Connection Failure - HTTP Return Code: {}\nResponse: {}'.format(response.status_code, response.text))
exit()
####################
# !!! DO WORK !!! #
####################
if __name__ == "__main__":
'''Gather all exporters and export them to a CSV file.'''
# If not hard coded, get the SMC IP, Username and Password
if not SW_SMC_ADDRESS:
SW_SMC_ADDRESS = input("SMC IP/FQDN Address: ")
if not SW_USERNAME:
SW_USERNAME = input("SMC Username: ")
if not SW_PASSWORD:
SW_PASSWORD = getpass.getpass("SMC Password: ")
# If a Domain ID wasn't specified, then get one
if SW_DOMAIN_ID is None:
# Authenticate to REST API
getAccessToken()
# Get Tenants from REST API
getTenants()
# Get all exporters from the SMC
exporters = getExporters()
# Write all the Exporters to CSV
with open('Exporter_IP_Output.csv', 'w') as csvoutput:
# Set up the CSV output
csv_writer = csv.writer(csvoutput)
# Print out all the exporter IPs
for exporter in exporters:
print(exporter['ipAddress'])
csv_writer.writerow([exporter['ipAddress']])