-
Notifications
You must be signed in to change notification settings - Fork 2
/
mymod.py
360 lines (273 loc) · 13.3 KB
/
mymod.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
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
#!/usr/bin/python
from __future__ import print_function
import paramiko
import StringIO
import sys, getopt
import time
import argparse
import socket
import re
from myobject import *
from creds import *
OLT = [name for name in olts.keys()]
PROFILE = [name for name in objects.keys()]
def check_arg(args=None):
"""Command line argumnet parser function"""
parser = argparse.ArgumentParser(description='OLT PON COMMAND LINE UTILITY')
mand = parser.add_argument_group(title='mandatory arguments')
mand.add_argument('-o', '--olt', choices = OLT, help=', '.join(OLT), metavar='', required='True')
mand.add_argument('-p', '--profile', choices = PROFILE, help=', '.join(PROFILE), metavar='', required='True')
opt = parser.add_argument_group("mandatory arguments only for add_onu, remove_onu")
opt.add_argument('-s', '--sn', help='ONU serial number', metavar='', nargs="?")
opt.add_argument('-l', '--loc', help='ONU location', metavar='', nargs="?")
args = parser.parse_args(args)
return (args.olt, args.profile, args.sn, args.loc)
def olt_profile(check_arg):
"""OLT and object profile function"""
olt_arg, profile_arg, sn, loc = check_arg(sys.argv[1:])
if olt_arg in OLT:
olt = olts[olt_arg]
else:
print ("No OLT specified")
if profile_arg in PROFILE:
profile = objects[profile_arg]
else:
profile = profile_arg
print ("No Object profile specified")
return (olt, profile, sn, loc)
class Olt:
"""Represents a OLT manipulation class"""
def __init__(self, olt, profile=None):
self.host = olt['host']
self.user = olt['user']
self.passwd = olt['passwd']
if profile is not None:
self.slot = profile['slot']
self.pon = profile['pon']
self.profileID = profile['profileID']
self.pppoeID = profile['pppoeID']
self.mgmtID = profile['mgmtID']
self.igmpID = profile['igmpID']
self.mcastID = profile['mcastID']
self.mgmtUP = profile['mgmtUP']
self.igmpUP = profile['igmpUP']
self.pppoeUP = profile['pppoeUP']
self.mcastPKG = profile['mcastPKG']
else:
pass
def find_bck(self, remote_conn):
cmd = 'backup-manager/show'
output = self.send_command(remote_conn, cmd)
backups = []
buf=StringIO.StringIO(output)
lines = buf.read().split("\n")
file_regex = re.compile(r".*({}).*".format('file'))
for line in lines:
mo_bck = file_regex.search(line)
if mo_bck:
filename = mo_bck.group().split('|')[1].strip()
backups.append(filename)
else:
pass
return backups
def find_next_onu(self, remote_conn):
self.nextonu = 0
onu_id_list = []
cmd = "remote-eq/discovery/show --slot=" + self.slot + " --port=" + self.pon
output = self.send_command(remote_conn, cmd)
time.sleep(1)
buf=StringIO.StringIO(output)
lines = buf.read().split("\n")
slot_regex = re.compile(r".*({}).*".format('PON'))
for line in lines[3:]:
mo_slot = slot_regex.search(line)
if mo_slot:
row = mo_slot.group().split('|')
if row[3].strip() != '--':
onu_id_list.append(int(row[3]))
else:
pass
if len(onu_id_list) == 0:
self.nextonu = 0
else:
onu_id_list.sort(key=int)
self.nextonu = onu_id_list[-1] + 1
return self.nextonu
def send_command(self, remote_conn, cmd=''):
if not cmd:
print ("Sending cmd: Enter", end='')
paddle = 85
else:
paddle = 90 - len(cmd)
print ("Sending cmd: " + cmd, end='')
cmd = cmd.rstrip()
remote_conn.send(cmd + '\n')
time.sleep(1)
output = remote_conn.recv(50000)
buf=StringIO.StringIO(output)
line = buf.read().split("\n")
for row in line:
row = row.split('|')
is_error = next((s for s in row if ('Error' in s or 'ERR' in s)), None)
if is_error:
break
if is_error:
print (', '.join(row))
sys.exit()
else:
message = "OK!"
print (message.rjust(paddle))
return output
def connect(self):
try:
# Create instance of SSHClient object
remote_conn_pre = paramiko.SSHClient()
# Automatically add untrusted hosts
remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# initiate SSH connection
remote_conn_pre.connect(self.host, username=self.user, password=self.passwd, timeout=5, look_for_keys=False, allow_agent=False)
cmd = "SSH connection established to " + self.host
message = "OK!"
paddle = 103 - len(cmd)
#print ("SSH connection established to %s" % host, end = '')
print (cmd, end = '')
print (message.rjust(paddle))
return remote_conn_pre
except paramiko.AuthenticationException:
print ("Authentication failed when connecting to %s" % host)
sys.exit(1)
except socket.timeout:
sys.exit("Connection timeout")
def find_onu_id(self, remote_conn, onu_sn):
cmd = "remote-eq/discovery/show --matching-serial-number=" + onu_sn + " --slot=" + self.slot + " --port=" + self.pon
onuid = None
profileid = None
output = self.send_command(remote_conn, cmd)
buf=StringIO.StringIO(output)
lines = buf.read().split("\n")
sn_regex = re.compile(".*({}).*".format(onu_sn))
for line in lines[3:]:
mo_sn = sn_regex.search(line)
if mo_sn:
row = mo_sn.group().split('|')
onuid = row[3]
profile = row[8][1]
profileid = profile[0]
break
else:
pass
if onuid is None:
sys.exit('!!!ONU ID not found!!!')
print (onuid)
else:
print (onuid)
return (onuid, profileid)
def find_onu_services(self, remote_conn, onuid):
cl_srv = {}
cl_srv_ids = []
net_srv_ids = []
cmd = "remote-eq/onu/services/show --slot=" + self.slot + " --port=" + self.pon + " --onuID=" + onuid
output = self.send_command(remote_conn, cmd)
buf=StringIO.StringIO(output)
lines = buf.read().split("\n")
srv_regex = re.compile(r".*\(\d+\).*")
for line in lines[3:]:
mo_srv = srv_regex.search(line)
if mo_srv:
row = mo_srv.group().split('|')
cl_srv_id = int(row[1])
cl_srv.setdefault(cl_srv_id, {})
cl_srv[cl_srv_id]['net_srv'] = int(row[4][1])
cl_srv[cl_srv_id]['srv_name'] = row[3]
cl_srv[cl_srv_id]['uni_ctag'] = int(row[10])
cl_srv[cl_srv_id]['ip_mgmt'] = 1 if row[11].strip() == 'enable' else 0
cl_srv[cl_srv_id]['up_profile'] = int(row[5][1]) if row[5][1] != '-' else 0
else:
pass
return cl_srv
def find_igmp_tag(self, remote_conn, onuid, srv_id):
is_igmp = False
cmd = "remote-eq/onu/services/show --slot=" + self.slot + " --port=" + self.pon + " --onuID=" + onuid + " --serviceID-onu=" + str(srv_id)
output = self.send_command(remote_conn, cmd)
buf=StringIO.StringIO(output)
lines = buf.read().split("\n")
igmp_regex = re.compile(r".*IGMP.*")
for line in lines:
mo_igmp = igmp_regex.search(line)
if mo_igmp:
is_igmp = True
else:
pass
return is_igmp
def change_upstream_profile(self, remote_conn, onuid, srv_id):
cmd = "remote-eq/onu/services/config --slot=" + self.slot + " --port=" + self.pon + " --onuID=" + onuid + " --serviceID-onu=" + str(srv_id) + " --upstream-dba-profile-id=5"
output = self.send_command(remote_conn, cmd)
def remove_onu_service(self, remote_conn, onuid, srvid):
cmd = "remote-eq/onu/services/remove --slot=" + self.slot + " --port=" + self.pon + " --onuID=" + str(onuid) + " --serviceID-onu=" + str(srvid)
#print (cmd)
output = self.send_command(remote_conn, cmd)
def remove_mcast_pkg(self, remote_conn, onuid, srvid):
cmd = "remote-eq/onu/services/mcast-package/remove --slot=" + self.slot + " --port=" + self.pon + " --onuID=" + str(onuid) + " --serviceID-onu=" + str(srvid) + " --pkg-id=1"
output = self.send_command(remote_conn, cmd)
def remove_onu(self, remote_conn, onuid):
cmd = "remote-eq/discovery/remove --slot=" + self.slot + " --port=" + self.pon + " --onuID=" + str(onuid)
#print (cmd)
output = self.send_command(remote_conn, cmd)
def create_commands(self, sn, loc, onu_id):
sn = sn
onuid = str(onu_id)
create_onu = "remote-eq/discovery/create --serial-number=" + sn + " --port=" + self.pon + " --slot=" + self.slot + " --onuID=" + onuid + " --admin=enable --profileID=" + self.profileID + " --register-type=serial-number --sw-upgrade-mode=auto"
add_loc = "/remote-eq/onu/system/config --location=" + loc + " --port=" + self.pon + " --slot=" + self.slot + " --onuID=" + onuid
add_mgmt = "remote-eq/onu/services/add --serviceID=" + self.mgmtID + " --port=" + self.pon + " --slot=" + self.slot + " --onuID=" + onuid + " --add-onu-port=veip.1 --encryption=disable --upstream-dba-profile-id=" + self.mgmtUP + " --admin=enable --ip-mgmt=enable --name=ip-mgmt --serviceID-onu=1"
add_pppoe = "remote-eq/onu/services/add --serviceID=" + self.pppoeID + " --port=" + self.pon + " --slot=" + self.slot + " --onuID=" + onuid + " --add-onu-port=veip.1 --encryption=disable --upstream-dba-profile-id=" + self.pppoeUP + " --admin=enable --name=internet-pppoe --serviceID-onu=2"
add_igmp = "remote-eq/onu/services/add --serviceID=" + self.igmpID + " --port=" + self.pon + " --slot=" + self.slot + " --onuID=" + onuid + " --add-onu-port=veip.1 --encryption=disable --upstream-dba-profile-id=" + self.igmpUP + " --admin=enable --name=iptv-igmp --serviceID-onu=3"
add_mcast = "remote-eq/onu/services/add --serviceID=" + self.mcastID + " --port=" + self.pon + " --slot=" + self.slot + " --onuID=" + onuid + " --add-onu-port=veip.1 --admin=enable --name=iptv-multicast --serviceID-onu=4"
add_mcast_pkg = "remote-eq/onu/services/mcast-package/create --onuID=" + onuid + " --port=" + self.pon + " --slot=" + self.slot + " --serviceID-onu=3 --pkg-id=" + self.mcastPKG
return (create_onu,
add_loc,
add_mgmt,
add_pppoe,
add_igmp,
add_mcast,
add_mcast_pkg)
def find_onu_sn(remote_conn, slot, pon):
sns = []
cmd = "remote-eq/discovery/show --slot=" + slot + " --port=" + pon
output = send_command(remote_conn, cmd)
buf=StringIO.StringIO(output)
print (buf)
lines = buf.read().split("\n")
sn_regex = re.compile(r".*({}).*".format('PON'))
for line in lines[3:]:
mo_sn = sn_regex.search(line)
if mo_sn:
row = mo_sn.group().split('|')
if row[3].strip() != '--':
sns.append(row[6])
else:
pass
return sns
def add_onu(remote_conn, slot, pon, sn, newonuid, profileid):
newonuid = str(newonuid)
cmd = "remote-eq/discovery/create --serial-number=" + sn + " --port=" + pon + " --slot=" + slot + " --onuID=" + newonuid + " --admin=enable --profileID=" + profileid + " --register-type=serial-number --sw-upgrade-mode=auto"
#print (cmd)
output = send_command(remote_conn, cmd)
def add_service(remote_conn, slot, pon, sn, newonuid, serviceid, upprofile, service_name, ipmgmt, unictag):
newonuid = str(newonuid)
serviceid = str(serviceid)
if ipmgmt:
upprofile = str(upprofile)
cmd = "remote-eq/onu/services/add --serviceID=" + serviceid + " --port=" + pon + " --slot=" + slot + " --onuID=" + newonuid + " --add-onu-port=veip.1 --encryption=disable --upstream-dba-profile-id=" + upprofile + " --admin=enable --name=" + service_name + " --serviceID-onu=" + serviceid + " --ip-mgmt=enable"
elif upprofile:
upprofile = str(upprofile)
cmd = "remote-eq/onu/services/add --serviceID=" + serviceid + " --port=" + pon + " --slot=" + slot + " --onuID=" + newonuid + " --add-onu-port=veip.1 --encryption=disable --upstream-dba-profile-id=" + upprofile + " --admin=enable --name=" + service_name + " --serviceID-onu=" + serviceid
else:
cmd = "remote-eq/onu/services/add --serviceID=" + serviceid + " --port=" + pon + " --slot=" + slot + " --onuID=" + newonuid + " --add-onu-port=veip.1 --admin=enable --name=" + service_name + " --serviceID-onu=" + serviceid
output = send_command(remote_conn, cmd)
if unictag == 40:
cmd = "remote-eq/onu/services/mcast-package/create --onuID=" + newonuid + " --port=" + pon + " --slot=" + slot + " --serviceID-onu=" + serviceid + " --pkg-id=1"
output = send_command(remote_conn, cmd)
else:
pass
if __name__ == "__main__":
main()