-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaxi_sync.py
301 lines (240 loc) · 9.17 KB
/
axi_sync.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
296
297
298
299
300
301
#!/usr/bin/python
# requirements
import socket
import logging
import datetime
import xml.dom.minidom
from ConfigParser import SafeConfigParser
import os
import ssl
import time
import xml.etree.ElementTree as ET
"""
Title: AXI_Sync
----------------------
Script to connect with SIP-DECT system(s) via AXI
and perform, configuration, update or inventory operations.
About:
------
This tool connects via AXI to a given list of SIP-DECT systems (OMMs).
Depending on the configured mode the following actions can be performed:
- inventory: log some system information (good to test script as no changes are performed)
- operation: (none, setconfig, update)
# none = no operation, good for test
# setconfig = Set the ConfigURL + Credentials if provided
# update = Initiate a software and configuration update request. (Update Button)
"""
__author__ = "Julian Zelina"
__version__ = "0.1"
__name__ = "AXI_Sync"
# enable debug
debug = True
myfolder = os.path.dirname(os.path.realpath(__file__))
# logging
today = datetime.date.today()
logger = logging.getLogger("AXI-Sync")
logger.setLevel(logging.INFO)
# create a file handler
handler = logging.FileHandler(myfolder + '/logs/' + str(today) + '.log')
handler.setLevel(logging.INFO)
# create a logging format
formatter = logging.Formatter('%(asctime)s - %(message)s')
handler.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(handler)
############################
# function
def echo_debug(text, isdebug):
logger.info(text)
if isdebug:
print(text)
def get_updatemin():
tmp_count = 0
tmp_minute = {}
for index in range(0, 60):
tmp_minute[tmp_count] = index
tmp_count += 1
return tmp_minute
def get_response(self):
# echo_debug("AXI.recv", debug)
mybuffer = b''
while b'\x00' not in mybuffer:
r = self.recv(4096)
if not r:
break
mybuffer += r
pos = mybuffer.find(b'\x00')
if pos >= 0:
last_response = mybuffer[:pos]
mybuffer = mybuffer[pos + 1:]
# echo_debug(("<< ", last_response), debug)
return xml.dom.minidom.parseString(last_response)
return None
############################
echo_debug("\n\nSTART {0}".format(str(__name__) + " - " + str(__version__)), debug)
# import configuration
echo_debug("Import configuration", debug)
config = SafeConfigParser()
config.read(myfolder + '/config/config.cfg')
# config file values
debug = bool(config.get('config', 'debug'))
username = str(config.get('config', 'username'))
password = str(config.get('config', 'password'))
inventory = bool(config.get('config', 'inventory'))
mode = str(config.get('config', 'mode'))
systemscfg = str(config.get('config', 'systems'))
stream = "ssl" # Use "tcp" or "ssl" connection to OMM.
ConfigURL = str(config.get('config', 'ConfigURL'))
Credentials = str(config.get('config', 'Credentials'))
UpdateHour = str(config.get('config', 'UpdateHour'))
delay = int(config.get('config', 'delay'))
echo_debug("Inventory: " + str(inventory), debug)
# start script
# check if mode is valid
if mode == "none" or mode == "update" or mode == "setconfig":
echo_debug("Mode: " + mode, debug)
else:
echo_debug("Mode: " + mode, debug)
echo_debug("No valid mode configured. (none,update,setconfig)", debug)
quit()
# read list of systems
with open(myfolder + systemscfg) as f:
lines = f.readlines()
echo_debug("Number of Lines: {0}".format(str(len(lines))), debug)
# read system list
# expected format:
# OMM-IP;
item = ""
systems = []
sysminute = {}
skipped = 0
if len(lines) > 0:
for item in lines:
# echo_debug("read line: " + item, debug)
tmp_val = item.split(";")
try:
# TODO: validate if input is an IP-Address
if tmp_val[0].count(".") == 3:
echo_debug("-> Add system to list: " + tmp_val[0], debug)
systems.append(tmp_val[0].rstrip())
if isinstance(int(tmp_val[1]), (int, long)):
if int(tmp_val[1].rstrip()) < 60:
sysminute[tmp_val[0].rstrip()] = tmp_val[1].rstrip()
else:
skipped += 1
echo_debug("-> Ignore this line.", debug)
except:
skipped += 1
echo_debug("-> Ignore this line by except.", debug)
else:
echo_debug("Systems list is empty!!. Exit script.", debug)
quit()
# print systems
echo_debug("Number of SIP-DECT systems: {0}".format(str(len(systems))), debug)
echo_debug("Number of SIP-DECT minute timers: {0}".format(str(len(sysminute))), debug)
if len(systems) == 0:
echo_debug("Systems list is empty!!. Exit script.", debug)
quit()
# calculate update timer minute from range based on system size.
update_minute = get_updatemin()
update_count = 0
# connect to OMM
# for each OMM
for ommip in systems:
try:
echo_debug("--------------------------", debug)
echo_debug("Connect to OMM: " + ommip, debug)
if stream == "tcp":
# Establish TCP connection
echo_debug("Establish TCP! connection, non SSL!!", debug)
s = socket.create_connection((ommip, 12621), 5)
s.settimeout(5)
else:
# Establish secure SSL without validations.
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(5)
s = ssl.wrap_socket(sock, keyfile=None, certfile=None, server_side=False)
s.connect((ommip, 12622))
# login
MESSAGE = '<Open seq="150" protocolVersion="41" username="' + username + '" password="' + password + '" />\0'
s.send(MESSAGE)
data = get_response(s)
root = ET.fromstring(data.toxml())
echo_debug("OMM Version: " + root.attrib['ommVersion'], debug)
# inventory
if inventory is True:
# inventory: PhoneState, Heath,
# GetSystemName
MESSAGE = '<GetSystemName />\0'
s.send(MESSAGE)
data = get_response(s)
root = ET.fromstring(data.toxml())
echo_debug("SysName: " + root.attrib['name'], debug)
# GetSystemName
MESSAGE = '<GetConfigURL />\0'
s.send(MESSAGE)
data = get_response(s)
echo_debug("Current ConfigURL: " + data.toxml(), debug)
# HealthState
MESSAGE = '<GetHealthState />\0'
s.send(MESSAGE)
data = get_response(s)
echo_debug("HealthState: " + data.toxml(), debug)
# User Summary
MESSAGE = '<GetPPUserSummary />\0'
s.send(MESSAGE)
data = get_response(s)
echo_debug("GetPPUserSummary: " + data.toxml(), debug)
# RFP Summary
MESSAGE = '<GetRFPSummary />\0'
s.send(MESSAGE)
data = get_response(s)
echo_debug("GetRFPSummary: " + data.toxml(), debug)
# action
# mode: none, setconfig, update
if mode == "none":
echo_debug("No mode defined, no action will be initiated.", debug)
if mode == "update":
# Trigger reload of configuration and software
echo_debug("Send update request.", debug)
MESSAGE = '<SoftwareUpdate />\0'
s.send(MESSAGE)
data = get_response(s)
# print "received data:", data.toxml()
if mode == "setconfig":
echo_debug("Set the ConfigURL.", debug)
if ConfigURL:
MESSAGE = ConfigURL + '\0'
s.send(MESSAGE)
data = get_response(s)
echo_debug("ConfigURL Response: " + data.toxml(), debug)
if Credentials:
MESSAGE = Credentials + '\0'
s.send(MESSAGE)
data = get_response(s)
echo_debug("Credential Response: " + data.toxml(), debug)
if UpdateHour:
try:
if sysminute[ommip]:
echo_debug("Minute set for system in ip.csv: " + sysminute[ommip] + "\n", debug)
MESSAGE = '<SetSystemProvUpdTrig enable="1" hour="' + str(UpdateHour) + '" minute="' + str(
sysminute[ommip]) + '" />' + '\0'
except:
MESSAGE = '<SetSystemProvUpdTrig enable="1" hour="' + str(UpdateHour) + '" minute="' + str(
update_minute[update_count]) + '" />' + '\0'
s.send(MESSAGE)
data = get_response(s)
echo_debug("SetSystemProvUpdTrig Response: " + data.toxml(), debug)
# close tcp connection
echo_debug("close connection to " + ommip + "\n", debug)
s.close()
# sleep delay timer
time.sleep(delay)
except:
echo_debug("ERROR could not connect to " + ommip, debug)
# increase update timer count (0..59, than 0..59)
update_count += 1
if update_count >= 60:
update_count = 0
# end loop
# exit