forked from PresidioCode/cucm-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cucm-exporter.py
295 lines (258 loc) · 8.29 KB
/
cucm-exporter.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import csv
import json
import sys
import os
from pathlib import Path
from datetime import datetime
from ciscoaxl import axl
from ciscoris import ris
import argparse
from email_util import send_email
from gooey import Gooey, GooeyParser
import cucm
BASE_DIR = Path(__file__).resolve().parent
IMG_DIR = BASE_DIR.joinpath("img")
start_time = datetime.now()
print(f"status: starting {start_time}")
class Unbuffered(object):
# GOOEY config -> ensure unbuffered output mode
def __init__(self, stream):
self.stream = stream
def write(self, data):
self.stream.write(data)
self.stream.flush()
def writelines(self, datas):
self.stream.writelines(datas)
self.stream.flush()
def __getattr__(self, attr):
return getattr(self.stream, attr)
sys.stdout = Unbuffered(sys.stdout)
# GOOEY config -> GUI if no cli args, otherwise default to cli
if len(sys.argv) >= 2:
if not "--ignore-gooey" in sys.argv:
sys.argv.append("--ignore-gooey")
def get_fieldnames(content):
"""
Return the longest Dict Item for csv header writing
"""
item_length = 0
csv_header = []
for item in content:
if len(item) >= item_length:
longest_item = item
item_length = len(item)
for key in longest_item.keys():
if key not in csv_header:
csv_header.append(key)
return csv_header
def output_filename(filename, cli_args):
"""
Construct the output filename
"""
if cli_args.timestamp:
date_time = datetime.now().strftime("%m-%d-%Y_%H.%M.%S")
lname = filename.split(".")[0]
rname = filename.split(".")[-1]
new_filename = f"{lname}_{date_time}.{rname}"
else:
new_filename = filename
return new_filename
def write_csv(filename, cli_args, content):
"""
write output to csv file
"""
filename = output_filename(filename, cli_args)
with open(filename, "w", newline="", encoding="utf-8") as csvfile:
fieldnames = get_fieldnames(content)
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for each in content:
writer.writerow(each)
return filename
@Gooey(
program_name="CUCM Extraction Tool",
program_description="Cisco Unified Communications Manager Tool",
# default_size=(610, 850),
menu=[
{"name": "File", "items": []},
{"name": "Tools", "items": []},
{
"name": "Help",
"items": [
{
"type": "Link",
"menuTitle": "Find us on Github",
"url": "https://github.com/bradh11/cucm-exporter",
}
],
},
],
# image_dir=IMG_DIR,
tabbed_groups=True,
)
def main():
date_time = datetime.now().strftime("%m-%d-%Y_%H.%M.%S")
# initialize the CLI parser
parser = GooeyParser(
description="Cisco Unified Communications Manager Tool")
cucm_group = parser.add_argument_group(title="cucm connection")
file_group = parser.add_argument_group(title="output file")
email_group = parser.add_argument_group(
title="optional email parameters",
description="send the output to an email address",
)
cucm_group.add_argument(
"--address",
"-a",
action="store",
dest="cucm_address",
help="specify cucm address",
default="ucm1.presidio.cloud",
required=True,
)
cucm_group.add_argument(
"--version",
"-v",
action="store",
dest="cucm_version",
choices=["8.5", "10.0", "10.5", "11.0", "11.5", "12.0", "12.5"],
help="specify cucm AXL version",
required=False,
default="11.0",
)
cucm_group.add_argument(
"--username",
"-u",
action="store",
dest="cucm_username",
help="specify ucm account username with AXL permissions",
required=True,
default="Administrator",
)
cucm_group.add_argument(
"--password",
"-p",
action="store",
dest="cucm_password",
help="specify ucm account password",
required=True,
default="Dev@1998",
widget="PasswordField",
)
file_group.add_argument(
"--out",
"-o",
action="store",
dest="filename",
help='filename of export file (.csv format) - default="export.csv"',
required=False,
default="export.csv",
)
file_group.add_argument(
"--timestamp",
"-t",
action="store_true",
dest="timestamp",
help="append filename with timestamp",
)
cucm_group.add_argument(
"--export",
"-e",
action="store",
dest="cucm_export",
choices=["users", "phones", "translations",
"sip-trunks", "registered-phones"],
help="specify what you want to export",
required=False,
default="users",
)
email_group.add_argument(
"--smtpserver",
"-s",
action="store",
dest="smtpserver",
required=False,
help="smtp server name or ip address",
)
email_group.add_argument(
"--mailto",
"-m",
action="store",
dest="mailto",
required=False,
help="send output to mail recipient",
)
# update variables from cli arguments
cli_args = parser.parse_args()
filename = cli_args.filename
# print(cli_args)
# store the UCM details
cucm_address = cli_args.cucm_address
cucm_username = cli_args.cucm_username
cucm_password = cli_args.cucm_password
cucm_version = cli_args.cucm_version
# initialize Cisco AXL connection
ucm_axl = axl(
username=cucm_username,
password=cucm_password,
cucm=cucm_address,
cucm_version=cucm_version,
)
# TODO: Add RIS connection as separate credentials
# ucm_ris = ris(
# username=cucm_username,
# password=cucm_password,
# cucm=cucm_address,
# cucm_version=cucm_version,
# )
if cli_args.cucm_export == "users":
output = cucm.export_users(ucm_axl)
if len(output) > 0:
saved_file = write_csv(
filename=filename, cli_args=cli_args, content=output)
else:
print(f"status: no {cli_args.cucm_export} found...")
print(f"status: elapsed time -- {datetime.now() - start_time}\n")
elif cli_args.cucm_export == "phones":
output = cucm.export_phones(ucm_axl)
if len(output) > 0:
saved_file = write_csv(
filename=filename, cli_args=cli_args, content=output)
else:
print(f"status: no {cli_args.cucm_export} found...")
print(f"status: elapsed time -- {datetime.now() - start_time}\n")
elif cli_args.cucm_export == "translations":
output = cucm.export_translations(ucm_axl)
if len(output) > 0:
saved_file = write_csv(
filename=filename, cli_args=cli_args, content=output)
else:
print(f"status: no {cli_args.cucm_export} found...")
print(f"status: elapsed time -- {datetime.now() - start_time}\n")
elif cli_args.cucm_export == "sip-trunks":
output = cucm.export_siptrunks(ucm_axl)
if len(output) > 0:
saved_file = write_csv(
filename=filename, cli_args=cli_args, content=output)
else:
print(f"status: no {cli_args.cucm_export} found...")
print(f"status: elapsed time -- {datetime.now() - start_time}\n")
else:
print(f"exporting {cli_args.cucm_export} is not yet supported")
return
# send email if selected
if cli_args.mailto and cli_args.smtpserver:
response = send_email(
smtp_server=cli_args.smtpserver,
send_to_email=cli_args.mailto,
fileToSend=saved_file,
)
print(
f"status: mail sent to {cli_args.mailto} via {cli_args.smtpserver} at {date_time} - {saved_file}"
)
elif cli_args.mailto and not cli_args.smtpserver:
print(f"status: mail unable to send. no smtp server was defined")
elif cli_args.smtpserver and not cli_args.mailto:
print(f"status: mail unable to send. no mailto address was defined")
if __name__ == "__main__":
main()