-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgateway_sip_trunk_ip_extractor.py
More file actions
440 lines (395 loc) · 17.5 KB
/
gateway_sip_trunk_ip_extractor.py
File metadata and controls
440 lines (395 loc) · 17.5 KB
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
"""
Multi-Cluster CUCM Gateway IP Report via AXL (v6)
Pulls IP addresses for H.323, MGCP, SIP Trunk, and SCCP Analog VG gateways.
Groups output by device type across multiple clusters into a single workbook.
Requirements:
pip install requests lxml openpyxl urllib3
"""
import re
import warnings
from collections import defaultdict
from datetime import datetime
import xml.etree.ElementTree as ET
import urllib3
import requests
from lxml import etree
from openpyxl import Workbook
from openpyxl.styles import Font, PatternFill, Alignment, Border, Side
from openpyxl.utils import get_column_letter
# ── CONFIGURATION ──────────────────────────────────────────────────
AXL_VERSION = "14.0" # Match your CUCM version
OUTPUT_FILE = "Gateway_Sip_Trunk_IP_Discovery_Report.xlsx"
CLUSTER_GROUPS = [
{
"user": "api_admin", # <-- UPDATE THIS
"pass": "your_password", # <-- UPDATE THIS
"clusters": [
{"name": "Cluster-US-East", "ip": "cucm-pub-01.example.local"},
{"name": "Cluster-US-West", "ip": "cucm-pub-02.example.local"},
]
}
]
# ───────────────────────────────────────────────────────────────────
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
SOAP_ENVELOPE = """<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns="http://www.cisco.com/AXL/API/{version}">
<soapenv:Header/>
<soapenv:Body>
{body}
</soapenv:Body>
</soapenv:Envelope>"""
def axl_sql_query(sql, host, user, pwd, label=""):
axl_url = f"https://{host}:8443/axl/"
headers = {
"Content-Type": "text/xml; charset=utf-8",
"SOAPAction": f"CUCM:DB ver={AXL_VERSION}",
}
body = f"<ns:executeSQLQuery><sql>{sql}</sql></ns:executeSQLQuery>"
payload = SOAP_ENVELOPE.format(version=AXL_VERSION, body=body)
try:
resp = requests.post(
axl_url, data=payload, headers=headers,
auth=(user, pwd), verify=False, timeout=60,
)
resp.raise_for_status()
tree = etree.fromstring(resp.content)
rows = []
for elem in tree.iter():
local = elem.tag.split("}")[-1] if "}" in elem.tag else elem.tag
if local == "row":
row = {}
for child in elem:
ctag = child.tag.split("}")[-1] if "}" in child.tag else child.tag
row[ctag] = child.text.strip() if child.text else ""
rows.append(row)
return rows
except Exception as e:
print(f" [{label}] Error: {e}")
return []
def risport_get_ips(device_names, host, user, pwd, status="Any"):
ris_url = f"https://{host}:8443/realtimeservice2/services/RISService70"
result = {}
batch_size = 200
for i in range(0, len(device_names), batch_size):
batch = device_names[i:i + batch_size]
items_xml = ""
for name in batch:
items_xml += f"<soap:item><soap:Item>{name}</soap:Item></soap:item>\n"
payload = f"""<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://schemas.cisco.com/ast/soap">
<soapenv:Header/>
<soapenv:Body>
<soap:selectCmDevice>
<soap:StateInfo></soap:StateInfo>
<soap:CmSelectionCriteria>
<soap:MaxReturnedDevices>1000</soap:MaxReturnedDevices>
<soap:DeviceClass>Any</soap:DeviceClass>
<soap:Model>255</soap:Model>
<soap:Status>{status}</soap:Status>
<soap:NodeName></soap:NodeName>
<soap:SelectBy>Name</soap:SelectBy>
<soap:SelectItems>
{items_xml} </soap:SelectItems>
<soap:Protocol>Any</soap:Protocol>
<soap:DownloadStatus>Any</soap:DownloadStatus>
</soap:CmSelectionCriteria>
</soap:selectCmDevice>
</soapenv:Body>
</soapenv:Envelope>"""
try:
resp = requests.post(
ris_url, data=payload,
headers={"Content-Type": "text/xml", "SOAPAction": "selectCmDevice"},
auth=(user, pwd), verify=False, timeout=60,
)
resp.raise_for_status()
root = ET.fromstring(resp.text)
ns = {"ns": "http://schemas.cisco.com/ast/soap"}
for device in root.findall(".//ns:CmDevices/ns:item", ns):
name_elem = device.find("ns:Name", ns)
ip_elem = device.find(".//ns:IPAddress/ns:item/ns:IP", ns)
name = name_elem.text.strip() if name_elem is not None and name_elem.text else ""
ip = ip_elem.text.strip() if ip_elem is not None and ip_elem.text else ""
if name and ip and ip != "0.0.0.0":
result[name] = ip
except Exception as e:
print(f" [WARN] RisPort batch {i//batch_size + 1} failed: {e}")
return result
def axl_get_sip_trunk_destinations(trunk_name, host, user, pwd):
axl_url = f"https://{host}:8443/axl/"
headers = {
"Content-Type": "text/xml; charset=utf-8",
"SOAPAction": f"CUCM:DB ver={AXL_VERSION}",
}
body = f"""<ns:getSipTrunk>
<name>{trunk_name}</name>
<returnedTags>
<destinations>
<destination>
<addressIpv4/>
<port/>
<sortOrder/>
</destination>
</destinations>
<sipProfileName/>
<securityProfileName/>
</returnedTags>
</ns:getSipTrunk>"""
payload = SOAP_ENVELOPE.format(version=AXL_VERSION, body=body)
try:
resp = requests.post(
axl_url, data=payload, headers=headers,
auth=(user, pwd), verify=False, timeout=30,
)
resp.raise_for_status()
tree = etree.fromstring(resp.content)
destinations = []
sip_profile = ""
sec_profile = ""
for elem in tree.iter():
local = elem.tag.split("}")[-1] if "}" in elem.tag else elem.tag
if local == "addressIpv4" and elem.text:
destinations.append(elem.text.strip())
if local == "sipProfileName" and elem.text:
sip_profile = elem.text.strip()
if local == "securityProfileName" and elem.text:
sec_profile = elem.text.strip()
return {
"destinations": ", ".join(destinations) if destinations else "N/A",
"sip_profile": sip_profile,
"sec_profile": sec_profile,
}
except Exception as e:
return {"destinations": f"Error: {e}", "sip_profile": "", "sec_profile": ""}
def get_h323_gateways(cluster_name, host, user, pwd):
sql = """SELECT d.name as device_name, d.description,
dp.name as device_pool, css.name as css_name,
tp.name as product_type
FROM device d
INNER JOIN typeproduct tp ON d.tkmodel = tp.tkmodel
INNER JOIN devicepool dp ON d.fkdevicepool = dp.pkid
LEFT JOIN callingsearchspace css ON d.fkcallingsearchspace_restrict = css.pkid
WHERE tp.name LIKE '%H.323%'"""
rows = axl_sql_query(sql, host, user, pwd, "H323")
results = []
for row in rows:
name = row.get("device_name", "")
results.append({
"Cluster Name": cluster_name,
"Gateway Name": name,
"IP Address": name,
"Description": row.get("description", ""),
"Device Pool": row.get("device_pool", ""),
"CSS": row.get("css_name", ""),
"Product Type": row.get("product_type", ""),
})
return results
def get_mgcp_gateways(cluster_name, host, user, pwd):
sql = """SELECT DISTINCT d.name as device_name, d.description,
dp.name as device_pool, tp.name as product_type
FROM device d
INNER JOIN typeproduct tp ON d.tkmodel = tp.tkmodel
INNER JOIN devicepool dp ON d.fkdevicepool = dp.pkid
WHERE d.tkclass = 2
AND tp.name NOT LIKE '%H.323%'
ORDER BY d.name"""
rows = axl_sql_query(sql, host, user, pwd, "MGCP")
results = []
for row in rows:
name = row.get("device_name", "")
results.append({
"Cluster Name": cluster_name,
"Gateway Name": name,
"IP Address": name,
"Description": row.get("description", ""),
"Device Pool": row.get("device_pool", ""),
"Product Type": row.get("product_type", ""),
})
return results
def get_sip_trunks(cluster_name, host, user, pwd):
sql = """SELECT d.name as trunk_name, d.description,
dp.name as device_pool, css.name as css_name,
tp.name as product_type
FROM device d
INNER JOIN typeproduct tp ON d.tkmodel = tp.tkmodel
INNER JOIN devicepool dp ON d.fkdevicepool = dp.pkid
LEFT JOIN callingsearchspace css ON d.fkcallingsearchspace_restrict = css.pkid
WHERE tp.name = 'SIP Trunk'
ORDER BY d.name"""
rows = axl_sql_query(sql, host, user, pwd, "SIP_trunks")
if not rows:
sql2 = """SELECT d.name as trunk_name, d.description,
dp.name as device_pool, css.name as css_name,
tp.name as product_type
FROM device d
INNER JOIN typeproduct tp ON d.tkmodel = tp.tkmodel
INNER JOIN devicepool dp ON d.fkdevicepool = dp.pkid
LEFT JOIN callingsearchspace css ON d.fkcallingsearchspace_restrict = css.pkid
WHERE tp.name LIKE '%Trunk%'
AND tp.name NOT LIKE '%MTP%'
AND tp.name NOT LIKE '%Conference%'
AND tp.name NOT LIKE '%Music%'
ORDER BY d.name"""
rows = axl_sql_query(sql2, host, user, pwd, "SIP_trunks_broad")
results = []
for idx, row in enumerate(rows):
trunk_name = row.get("trunk_name", "")
detail = axl_get_sip_trunk_destinations(trunk_name, host, user, pwd)
results.append({
"Cluster Name": cluster_name,
"Trunk Name": trunk_name,
"Destination Address(es)": detail["destinations"],
"Description": row.get("description", ""),
"Device Pool": row.get("device_pool", ""),
"CSS": row.get("css_name", ""),
"SIP Profile": detail["sip_profile"],
"Security Profile": detail["sec_profile"],
})
return results
def decode_an_mac_portion(device_name):
raw = device_name.upper().replace("AN", "", 1)
if len(raw) >= 3:
mac_portion = raw[:-3]
mac_padded = mac_portion.ljust(12, "0")[:12]
mac_formatted = ":".join(mac_padded[i:i+2] for i in range(0, 12, 2))
return mac_portion, mac_formatted
return raw, raw
def get_analog_vg_ips(cluster_name, host, user, pwd):
sql = """SELECT d.name as device_name, d.description, dp.name as device_pool
FROM device d
INNER JOIN devicepool dp ON d.fkdevicepool = dp.pkid
WHERE d.name LIKE 'AN%'
AND d.tkclass = 1
ORDER BY d.description"""
rows = axl_sql_query(sql, host, user, pwd, "AN_devices")
vg_groups = defaultdict(list)
for row in rows:
name = row.get("device_name", "")
mac_portion, mac_formatted = decode_an_mac_portion(name)
vg_groups[mac_formatted].append({
"device_name": name,
"description": row.get("description", ""),
"device_pool": row.get("device_pool", ""),
"mac_portion": mac_portion,
})
sample_to_mac = {devices[0]["device_name"]: mac for mac, devices in vg_groups.items()}
all_samples = list(sample_to_mac.keys())
ris_ips = risport_get_ips(all_samples, host, user, pwd, status="Any") if all_samples else {}
vg_ip_map = {}
for sample_name, ip in ris_ips.items():
mac = sample_to_mac.get(sample_name)
if mac:
vg_ip_map[mac] = ip
results = []
for mac, devices in sorted(vg_groups.items()):
desc = devices[0].get("description", "")
vg_name = desc
port_match = re.search(r'\s+\d+/\d+/\d+\s*$', desc)
if port_match:
vg_name = desc[:port_match.start()].strip()
elif " " in desc:
vg_name = desc.rsplit(" ", 1)[0].strip()
if not vg_name:
vg_name = mac
ip = vg_ip_map.get(mac, "")
results.append({
"Cluster Name": cluster_name,
"VG Name": vg_name,
"IP Address": ip,
"VG MAC": mac,
"Sample AN Device": devices[0]["device_name"],
"Device Pool": devices[0]["device_pool"],
"Port Count": len(devices),
"Note": "Resolved via RIS" if ip else "Not registered / no siblings online"
})
return results
def write_excel(h323_data, mgcp_data, sip_data, vg_data):
wb = Workbook()
hf = Font(name="Arial", bold=True, color="FFFFFF", size=11)
hfill = PatternFill("solid", fgColor="2F5496")
ha = Alignment(horizontal="center", vertical="center", wrap_text=True)
cf = Font(name="Arial", size=10)
ca = Alignment(vertical="center", wrap_text=True)
tb = Border(left=Side(style="thin"), right=Side(style="thin"),
top=Side(style="thin"), bottom=Side(style="thin"))
af = PatternFill("solid", fgColor="D6E4F0")
def write_sheet(ws, title, headers, data):
ws.title = title
ws.sheet_properties.tabColor = "2F5496"
ws.freeze_panes = "A2"
for ci, h in enumerate(headers, 1):
c = ws.cell(row=1, column=ci, value=h)
c.font, c.fill, c.alignment, c.border = hf, hfill, ha, tb
for ri, rd in enumerate(data, 2):
for ci, h in enumerate(headers, 1):
v = rd.get(h, "")
c = ws.cell(row=ri, column=ci, value=v)
c.font, c.alignment, c.border = cf, ca, tb
if ri % 2 == 0:
c.fill = af
for ci in range(1, len(headers) + 1):
ml = len(str(headers[ci - 1]))
for row in ws.iter_rows(min_row=2, max_row=max(ws.max_row, 2),
min_col=ci, max_col=ci):
for cell in row:
if cell.value:
ml = max(ml, min(len(str(cell.value)), 60))
ws.column_dimensions[get_column_letter(ci)].width = ml + 4
if not data:
ws.cell(row=2, column=1, value="No data found").font = Font(
name="Arial", italic=True, color="888888")
write_sheet(wb.active, "H.323 Gateways",
["Cluster Name", "Gateway Name", "IP Address", "Description",
"Device Pool", "CSS", "Product Type"], h323_data)
write_sheet(wb.create_sheet(), "MGCP Gateways",
["Cluster Name", "Gateway Name", "IP Address", "Description",
"Device Pool", "Product Type"], mgcp_data)
write_sheet(wb.create_sheet(), "SIP Trunks",
["Cluster Name", "Trunk Name", "Destination Address(es)", "Description",
"Device Pool", "CSS", "SIP Profile", "Security Profile"],
sip_data)
write_sheet(wb.create_sheet(), "Analog VG (SCCP)",
["Cluster Name", "VG Name", "IP Address", "VG MAC", "Sample AN Device",
"Device Pool", "Port Count", "Note"], vg_data)
sws = wb.create_sheet("Summary", 0)
sws.sheet_properties.tabColor = "00B050"
sd = [
{"Gateway Type": "H.323 Gateways", "Total Count": len(h323_data)},
{"Gateway Type": "MGCP Gateways", "Total Count": len(mgcp_data)},
{"Gateway Type": "SIP Trunks", "Total Count": len(sip_data)},
{"Gateway Type": "Analog VG (SCCP)", "Total Count": len(vg_data)},
]
write_sheet(sws, "Summary", ["Gateway Type", "Total Count"], sd)
sws.sheet_properties.tabColor = "00B050"
ts = datetime.now().strftime("%Y-%m-%d %H:%M")
sws.cell(row=7, column=1, value=f"Generated: {ts}").font = Font(
name="Arial", italic=True, size=9, color="666666")
wb.save(OUTPUT_FILE)
print(f"\n[+] Global Report saved: {OUTPUT_FILE}")
def main():
print("=" * 60)
print(" Multi-Cluster Gateway & Trunk Discovery")
print("=" * 60)
master_h323 = []
master_mgcp = []
master_sip = []
master_vg = []
for group in CLUSTER_GROUPS:
axl_user = group["user"]
axl_pass = group["pass"]
for cluster in group["clusters"]:
cluster_name = cluster["name"]
cucm_ip = cluster["ip"]
print(f"\n[{cluster_name}] Connecting to {cucm_ip}...")
test = axl_sql_query("SELECT count(*) as total FROM device", cucm_ip, axl_user, axl_pass, "TEST")
if not test:
print(f" ❌ FAILED - Skipping {cluster_name}. Check IP/Credentials.")
continue
print(f" ✅ Connected. Total devices in DB: {test[0].get('total', '?')}")
master_h323.extend(get_h323_gateways(cluster_name, cucm_ip, axl_user, axl_pass))
master_mgcp.extend(get_mgcp_gateways(cluster_name, cucm_ip, axl_user, axl_pass))
master_sip.extend(get_sip_trunks(cluster_name, cucm_ip, axl_user, axl_pass))
master_vg.extend(get_analog_vg_ips(cluster_name, cucm_ip, axl_user, axl_pass))
write_excel(master_h323, master_mgcp, master_sip, master_vg)
if __name__ == "__main__":
main()