-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHostConfig.py
executable file
·434 lines (394 loc) · 16.7 KB
/
HostConfig.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
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
import time
import Validations
from MailConfiguration import Mail
from HostSSH import SSHClient
from terminaltables import AsciiTable
from DHandler import DbHandler
import getpass
from Constant import HostConstant
class HostOptions(SSHClient, DbHandler, Mail):
def numbers_to_options(self, argument):
if int(argument) == 1:
method_name = 'addHost'
elif int(argument) == 2:
method_name = 'removeHost'
elif int(argument) == 3:
method_name = 'runHost'
elif int(argument) == 4:
method_name = 'editHostConfiguration'
elif int(argument) == 5:
method_name = 'viewHost'
elif int(argument) == 6:
method_name = 'updateMail'
else:
print 'No options found'
return
method = getattr(self, method_name, lambda: "Invalid Options")
return method()
def addHost(self): # Add Host
try:
ipAddress = raw_input("Enter the host: ")
while not Validations.checkIsEmpty(ipAddress):
ipAddress = raw_input("Enter the valid ip address: ")
while not Validations.isServerUp(ipAddress):
ipHost = raw_input('The entered host is server down. Do you want to continue? (y/n)')
if ipHost == 'y':
break
else:
ipAddress = raw_input("Enter the valid ip address: ")
userName = raw_input("\nEnter userName: ")
while not Validations.checkIsEmpty(userName):
userName = raw_input("Please enter the username: ")
password = getpass.getpass("Enter password: ")
while not Validations.checkIsEmpty(password):
password = getpass.getpass('Please enter the password: ')
newServerData = {
'env':'cli',
'hostname': ipAddress,
'username': userName,
'password': password,
}
self.startProgress()
ssh, error = self.checkHost(newServerData)
if error:
checkHostOption = raw_input('Failed to connect the server, Do you want continue?(y/n)')
if checkHostOption == 'n':
return
ssh.close()
self.stopProgress()
port = raw_input("Enter port: ")
if not port:
port = '22'
dir_path = raw_input("Enter the directory path. (ex.:/home/user/): ")
while not Validations.checkIsEmpty(dir_path):
dir_path = raw_input('Please enter the directory path: ')
file_name = raw_input("(Optional) Enter the file name or extension. (ex:*.txt): ")
email = raw_input('Enter the email address for alerts.: ')
if email:
while not Validations.checkEmail(email):
email = raw_input('Enter the valid email address.: ')
if not email:
break
else:
email = ''
pwd = self.encryptpwd(password)
hostData = {
'hostname': ipAddress,
'username': userName,
'password': pwd,
'port': port,
'dir':dir_path,
'file_name':file_name,
'mail':email,
'fwatcher':'',
'is_watching':'No',
'conn_status':'Success'
}
self.saveData(hostData)
# self.createJsonFile()
except (IOError, KeyboardInterrupt):
return
def removeHost(self): # Remove host
try:
listsHost = []
hdetails = self.selectHostDetail()
if hdetails == []:
print 'No IP Address found, try Again'
return
table_data = []
table_data.append(['Options', 'Hosts'])
for data in hdetails:
table_data.append([str(data['did']), str(data['hostname'])])
listsHost.append(str(data['hostname']))
table = AsciiTable(table_data)
print table.table
host = raw_input('Enter the option to remove: ')
while not Validations.checkIsInteger(host):
host = raw_input("Enter the option to remove: ")
while not any(str(d['did']) == str(host) for d in hdetails):
host = raw_input("The host not found, Enter the valid option to remove: ")
choice = raw_input('Are you sure, you want to remove?(y/n)')
while not Validations.checkIsEmpty(choice):
choice = raw_input('Do you want to remove?(y/n)')
if choice == 'y':
self.removeHostServer(host)
except (IOError, IndexError, KeyboardInterrupt):
print 'Hosts not found'
def runHost(self):
try:
hdetails = self.selectHostDetail()
if hdetails == []:
print 'No IP Address found, try Again'
return
table_data = []
table_data.append(['Options', 'Hosts'])
table_data.append(['0','Run All'])
listIndex = []
listId = []
for index, data in enumerate(hdetails):
index = index +1
table_data.append([str(data['did']), str(data['hostname'])])
listIndex.append(str(index))
listId.append(str(data['did']))
table = AsciiTable(table_data)
print table.table
userAction = raw_input('Enter the option to connect host: ')
if Validations.checkIsInteger(userAction):
if int(userAction) == 0:
self.hostWatcherAll(hdetails)
else:
while not any(str(d['did']) == str(userAction) for d in hdetails):
userAction = raw_input("Enter the valid option to connect host: ")
self.hostWatcher(userAction)
else:
try:
numbers = userAction.split(',')
hdAll = []
for element in numbers:
# indices = [i for i, x in enumerate(hdetails) if str(x['did']) == element]
for ix, x in enumerate(hdetails):
if str(x['did']) == element:
hdAll.append(hdetails[ix])
if hdAll:
self.hostWatcherAll(hdAll)
else:
print 'The entered options are not available'
except TypeError:
print 'Enter the valid hosts.'
return
except (IndexError, IOError, KeyboardInterrupt, AttributeError, TypeError):
print 'Host not found'
return
def hostWatcher(self, index):
try:
hdetails = self.selectMethod(index)
self.updateWatcher('Yes', hdetails['hostname'])
self.startProgress()
while True:
self.connect_host(hdetails)
time.sleep(10)
except KeyboardInterrupt:
self.stopProgress()
hdetails = self.selectMethod(index)
self.updateFileData('','No', hdetails['hostname'])
print 'Host watching stopped'
return
def hostWatcherAll(self, list):
try:
for h in list:
self.updateWatcher('Yes', h['hostname'])
self.startProgress()
while True:
self.calculateParallel(list, len(list))
time.sleep(10)
except KeyboardInterrupt:
self.stopProgress()
for h in list:
self.updateFileData('','No', h['hostname'])
print 'Host watching stopped'
return
def editHostConfiguration(self):
try:
hdetails = self.selectHostDetail()
if hdetails == []:
print 'No IP Address found, try Again'
return
table_data = []
table_data.append(['Options', 'Hosts'])
for data in hdetails:
table_data.append([str(data['did']), str(data['hostname'])])
table = AsciiTable(table_data)
print table.table
userHostOption = raw_input('Select option to edit: ')
while not Validations.checkIsInteger(userHostOption):
userHostOption = raw_input("Select option to edit: ")
while not any(str(d['did']) == str(userHostOption) for d in hdetails):
userHostOption = raw_input("Select valid option to edit: ")
self.updateHostData(userHostOption)
except (IndexError, IOError, KeyboardInterrupt, AttributeError, TypeError):
print 'Configution not added.'
def viewHost(self):
try:
hdetails = self.selectHostDetail()
if hdetails == []:
print 'No IP Address found, try Again'
return
table_data = []
table_data.append(['Options', 'Hosts', 'Receiver'])
for data in hdetails:
table_data.append([str(data['did']), str(data['hostname']), str(data['mail'])])
table = AsciiTable(table_data)
print table.table
except (IOError, KeyboardInterrupt, AttributeError, TypeError):
print 'Configution not added.'
def updateMail(self):
try:
table_data = []
table_data.append(['Mail', 'values'])
smtpConfig = self.readSmtpData()
config = self.readMailData()
if config is None:
return
table_data.append(['SMTP', smtpConfig['smtp']])
table_data.append(['SMTP Port', smtpConfig['smtpPort']])
table_data.append(['Email', config['smtpMail']])
table_data.append(['Password',' **********'])
table_data.append(['Receiver', config['receiver']])
table_data.append(['Subject', config['subject']])
table = AsciiTable(table_data)
print table.table
print 'Do you want to edit following configuration?'
print '(1) SMTP'
print '(2) Mail'
print '(3) Exit'
uMailInput = raw_input('Enter the option: ')
while not Validations.checkIsInteger(uMailInput):
uMailInput = raw_input("Please enter the option: ")
if int(uMailInput) == 1:
self.configSmtp(self)
if int(uMailInput) == 2:
self.configMail(self,config['smtpMail'])
else:
return
except KeyboardInterrupt:
return
def removeHostServer(self, index):
try:
self.deleteData(index)
print 'Succesfully removed host.\n'
except (IndexError, IOError):
print 'Host not found'
def updateHostData(self, option):
try:
obj = self.selectMethod(option)
table_data = []
table_data.append(['Mail', 'Values'])
table_data.append(['Username', obj['username']])
table_data.append(['Password', ' *******'])
table_data.append(['Host', obj['hostname']])
table_data.append(['Port', obj['port']])
table_data.append(['Directory Path', obj['dir']])
table_data.append(['File Name', obj['file_name']])
table_data.append(['E-mail', obj['mail']])
table = AsciiTable(table_data)
print table.table
except IndexError:
print 'Host not exist'
print '---------------------'
print '(1) Username'
print '(2) Password'
print '(3) Host'
print '(4) Port'
print '(5) Directory Path'
print '(6) File Name'
print '(7) E-mail'
print '(8) All'
print '(9) Exit'
print '---------------------'
try:
userSelectedHost = raw_input('Select option to update: ')
while not Validations.checkIsInteger(userSelectedHost):
userSelectedHost = raw_input("Select option to update: ")
if int(userSelectedHost) == 1:
self.updateHostConfigs(option, 'Username', HostConstant.uname)
if int(userSelectedHost) == 2:
self.updateHostConfigs(option, 'Password', HostConstant.pwd)
if int(userSelectedHost) == 3:
self.updateHostConfigs(option, 'Host', HostConstant.host)
if int(userSelectedHost) == 4:
self.updateHostConfigs(option, 'Port', HostConstant.port)
if int(userSelectedHost) == 5:
self.updateHostConfigs(option, 'Directory Path', HostConstant.dirpath)
if int(userSelectedHost) == 6:
self.updateHostConfigs(option, 'File name', HostConstant.fname)
if int(userSelectedHost) == 7:
self.updateHostConfigs(option, 'E-mail', HostConstant.email)
if int(userSelectedHost) == 8:
self.updateAllConfigs(option)
else:
raise IndexError
except IndexError as e:
print e
def updateHostConfigs(self, index, obj, column):
if column == HostConstant.pwd:
userValue = getpass.getpass("Enter the " + obj + " value: ")
userValue = self.encryptpwd(userValue)
else:
userValue = raw_input("Enter the " + obj + " value: ")
if column == HostConstant.host:
while not Validations.checkIsEmpty(userValue):
userValue = raw_input("Enter the valid host: ")
while not Validations.isServerUp(userValue):
ipHost = raw_input('The entered host is server down. Do you want to continue? (y/n)')
if ipHost == 'y':
break
else:
userValue = raw_input("Enter the valid host: ")
elif column == HostConstant.email:
if userValue:
while not Validations.checkEmail(userValue):
userValue = raw_input('Enter the valid email address.: ')
if not userValue:
break
else:
userValue = ''
self.updateData(column, userValue, index)
def updateAllConfigs(self, index):
userNameValue = raw_input('Enter the username: ')
while not Validations.checkIsEmpty(userNameValue):
userNameValue = raw_input('Please enter the username: ')
passwordValue = getpass.getpass('Enter the password: ')
while not Validations.checkIsEmpty(passwordValue):
passwordValue = getpass.getpass('Please enter the password: ')
ipAddress = raw_input('Enter the Host: ')
while not Validations.checkIsEmpty(ipAddress):
ipAddress = raw_input("Enter the valid ip address: ")
while not Validations.isServerUp(ipAddress):
ipHost = raw_input('The entered host is server down. Do you want to continue? (y/n)')
if ipHost == 'y':
return
else:
ipAddress = raw_input("Enter the valid ip address: ")
hostData = {
'env': 'cli',
'hostname': ipAddress,
'username': userNameValue,
'password': passwordValue,
}
self.startProgress()
ssh, error = self.checkHost(hostData)
if error:
checkHostOption = raw_input('Failed to connect the server, Do you want continue?(y/n)')
if checkHostOption == 'n':
return
ssh.close()
self.stopProgress()
portValue = raw_input('Enter the port: ')
if not portValue:
portValue = '22'
dir_path = raw_input("Enter the directory path. (ex.:/home/user/): ")
while not Validations.checkIsEmpty(dir_path):
dir_path = raw_input('Please enter the directory path: ')
file_name = raw_input("(Optional)Enter the file name. (ex:*.txt): ")
if not file_name:
file_name = ''
email = raw_input("Enter the email: ")
if email:
while not Validations.checkEmail(email):
email = raw_input('Enter the valid email address.: ')
if not email:
break
else:
email = ''
pwd = self.encryptpwd(passwordValue)
updateServerData = {
'hostname': ipAddress,
'username': userNameValue,
'password': pwd,
'port': portValue,
'dir': dir_path,
'file_name':file_name,
'mail': email
}
self.updateAllData(updateServerData, index)
print 'Updated successfully.'