-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser.py
More file actions
481 lines (427 loc) · 20.6 KB
/
Copy pathbrowser.py
File metadata and controls
481 lines (427 loc) · 20.6 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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import time
from collections import namedtuple
from selenium.webdriver.support.select import Select
Input = namedtuple("Input","name,type") # type in ["checkBox","text","radio","select","autocomplete","click"]
Option = namedtuple("Option","value,text")
DELAY = 4 # Seconds (s)
differents = {
"service_hPars-t[]" : "service_hPars[]",
"service_cs-t[]" : "service_cs[]",
"service_cgs-t[]" : "service_cgs[]"
}
SERVICE_INPUTS = [
Input("service_description", "text"),
Input("service_template_model_stm_id","select"),
Input("service_is_volatile[service_is_volatile]","radio"),
Input("timeperiod_tp_id","select"),
Input("command_command_id","select"),
Input("ARG1","text"),
Input("ARG2","text"),
Input("ARG3","text"),
Input("ARG4","text"),
Input("ARG5","text"),
Input("ARG6","text"),
Input("ARG7","text"),
Input("ARG8","text"),
Input("ARG9","text"),
Input("ARG10","text"),
Input("service_max_check_attempts", "text"),
Input("service_normal_check_interval", "text"),
Input("service_retry_check_interval", "text"),
Input("service_active_checks_enabled[service_active_checks_enabled]","radio"),
Input("service_passive_checks_enabled[service_passive_checks_enabled]", "radio"),
Input("service_notifications_enabled[service_notifications_enabled]", "radio"),
Input("service_cs-t[]","autocomplete"),
Input("service_cgs-t[]", "autocomplete"),
Input("service_notification_interval", "text"),
Input("timeperiod_tp_id2", "select"),
Input("service_notifOpts[w]","checkBox"),
Input("service_notifOpts[u]","checkBox"),
Input("service_notifOpts[c]","checkBox"),
Input("service_notifOpts[r]","checkBox"),
Input("service_notifOpts[f]","checkBox"),
Input("service_notifOpts[s]","checkBox"),
Input("service_first_notification_delay","text"),
Input("li#c2 a","click"),
Input("service_hPars-t[]","autocomplete"),
Input("mnftr","select"),
Input("service_traps-t[]","autocomplete"),
Input("service_obsess_over_service[service_obsess_over_service]","radio"),
Input("li#c3 a","click"),
Input("service_check_freshness[service_check_freshness]", "radio"),
Input("service_freshness_threshold","text"),
Input("service_flap_detection_enabled[service_flap_detection_enabled]","radio"),
Input("service_low_flap_threshold","text"),
Input("service_high_flap_threshold","text"),
Input("service_retain_status_information[service_retain_status_information]","radio"),
Input("service_retain_nonstatus_information[service_retain_nonstatus_information]","radio"),
Input("service_stalOpts[o]","checkBox"),
Input("service_stalOpts[w]","checkBox"),
Input("service_stalOpts[u]","checkBox"),
Input("service_stalOpts[c]","checkBox"),
Input("service_event_handler_enabled[service_event_handler_enabled]","radio"),
Input("command_command_id2","select"),
Input("command_command_id_arg2","text"),
Input("li#c4 a","click"),
Input("graph_id","select"),
Input("service_categories-t[]","autocomplete"),
Input("esi_notes_url","text"),
Input("esi_notes","text"),
Input("esi_action_url","text"),
Input("esi_icon_image","select"),
Input("esi_icon_image_alt","text"),
Input("criticality_id","select"),
Input("service_activate[service_activate]","radio"),
Input("service_comment","textArea")
]
SERVICE_OUTPUTS = [
Input("service_description", "text"),
Input("service_template_model_stm_id","select"),
Input("service_is_volatile[service_is_volatile]","radio"),
Input("timeperiod_tp_id","select"),
Input("command_command_id","select"),
Input("ARG1","text"),
Input("ARG2","text"),
Input("ARG3","text"),
Input("ARG4","text"),
Input("ARG5","text"),
Input("ARG6","text"),
Input("ARG7","text"),
Input("ARG8","text"),
Input("ARG9","text"),
Input("ARG10","text"),
Input("service_hPars-t[]","autocomplete"),
Input("service_max_check_attempts", "text"),
Input("service_normal_check_interval", "text"),
Input("service_retry_check_interval", "text"),
Input("service_active_checks_enabled[service_active_checks_enabled]","radio"),
Input("service_passive_checks_enabled[service_passive_checks_enabled]", "radio"),
Input("li#c2 a","click"),
Input("service_notifications_enabled[service_notifications_enabled]", "radio"),
Input("service_cs-t[]","autocomplete"),
Input("service_cgs-t[]", "autocomplete"),
Input("service_notification_interval", "text"),
Input("timeperiod_tp_id2", "select"),
Input("service_notifOpts[w]","checkBox"),
Input("service_notifOpts[u]","checkBox"),
Input("service_notifOpts[c]","checkBox"),
Input("service_notifOpts[r]","checkBox"),
Input("service_notifOpts[f]","checkBox"),
Input("service_notifOpts[s]","checkBox"),
Input("service_first_notification_delay","text"),
Input("li#c3 a","click"),
Input("service_traps-t[]","autocomplete"),
Input("service_obsess_over_service[service_obsess_over_service]","radio"),
Input("li#c4 a","click"),
Input("service_check_freshness[service_check_freshness]", "radio"),
Input("service_freshness_threshold","text"),
Input("service_flap_detection_enabled[service_flap_detection_enabled]","radio"),
Input("service_low_flap_threshold","text"),
Input("service_high_flap_threshold","text"),
Input("service_retain_status_information[service_retain_status_information]","radio"),
Input("service_retain_nonstatus_information[service_retain_nonstatus_information]","radio"),
Input("service_stalOpts[o]","checkBox"),
Input("service_stalOpts[w]","checkBox"),
Input("service_stalOpts[u]","checkBox"),
Input("service_stalOpts[c]","checkBox"),
Input("service_event_handler_enabled[service_event_handler_enabled]","radio"),
Input("command_command_id2","select"),
Input("command_command_id_arg2","text"),
Input("li#c5 a","click"),
Input("graph_id","select"),
Input("service_categories-t[]","autocomplete"),
Input("esi_notes_url","text"),
Input("esi_notes","text"),
Input("esi_action_url","text"),
Input("esi_icon_image_alt","text"),
Input("service_activate[service_activate]","radio"),
Input("service_comment","textArea"),
Input("submitA","click")
]
HOST_INPUTS = [
Input("host_name","text"),
Input("dupSvTplAssoc[dupSvTplAssoc]","radio"),
Input("host_notifOpts[u]","checkBox"),
Input("nagios_server_id","select"),
Input("host_cgs-t[]","autocomplete")
]
HOST_OUTPUTS = [
Input("host_name","text"),
Input("dupSvTplAssoc","radio"),
Input("host_notifOpts[u]","checkBox"),
Input("nagios_server_id","select")
]
def pause():
print("[+] Pausa ...")
input("[-] Pulse Enter para continuar: ")
wait = lambda t: time.sleep(t)
check_host_pattern = lambda x: "&o=c&host_id=" in x
check_service_pattern = lambda x: "p=60201&o=c&service_id=" in x
class browser:
def __init__(self,url,item_offset = 0,page_number = 0) -> None:
self.item_offset = item_offset
self.page_number = page_number
self.base = url
self.driver = webdriver.Chrome('./chromedriver.exe')
self.driver.get(url)
self.urls = []
pause()
def get_Hosts_Urls(self):
elements = self.driver.find_elements_by_css_selector("td.ListColLeft a")
for element in elements:
if check_host_pattern(element.get_attribute('href')):
#print(element.get_attribute('href'))
if not element.get_attribute('href') in self.urls:
self.urls.append(element.get_attribute('href'))
return self.urls[self.item_offset:]
def get_all_Services_Urls(self,numPages):
result = set()
for i in range(0,numPages):
print("Leyendo página {i}")
self.driver.get("http://boreal.dipusevilla.es/boreal/main.php?p=60201&num={}&limit=30&poller=&template=0&search=&type=&o=&search_type_service=1&search_type_host=1&status=&hostgroups=0".format(i))
wait(DELAY)
urls = [url.get_attribute("href") for url in self.driver.find_elements_by_css_selector("td.ListColLeft a") if check_service_pattern(url.get_attribute("href"))]
result.update(set(urls))
return result
def get_all_new_Services_Urls(self,start,numPages):
result = set()
for i in range(start,numPages):
print("Leyendo página {}".format(i))
self.driver.get("http://10.1.255.50/centreon/main.php?p=60201&num={}".format(i))
wait(DELAY)
iframe = WebDriverWait(self.driver,30).until(lambda d: d.find_element_by_id("main-content"))
self.driver.switch_to.frame(iframe)
urls = [url.get_attribute("href") for url in self.driver.find_elements_by_css_selector("td.ListColLeft a") if check_service_pattern(url.get_attribute("href"))]
result.update(set(urls))
print(len(set(urls)))
return result
def get_Services_Urls(self):
last_host = ""
self.driver.get("http://boreal.dipusevilla.es/boreal/main.php?p=60201&num={}&limit=30&poller=&template=0&search=&type=&o=&search_type_service=1&search_type_host=1&status=&hostgroups=0".format(self.page_number))
wait(DELAY)
table = self.driver.find_elements_by_css_selector(".ListTable tr")
for tr in table:
children = tr.find_elements_by_css_selector("td.ListColLeft a")
for a in children:
if check_host_pattern(a.get_attribute('href')):
if not a.text in [""," "]:
last_host = a.text
elif check_service_pattern(a.get_attribute('href')):
self.urls.append((last_host,a.get_attribute('href')))
print((last_host,a.get_attribute('href')))
self.urls = self.urls[self.item_offset:]
return self.urls
def get_Input_Value(self,name,type):
try:
result = None
if type == "text":
result = None
if(len(self.driver.find_elements_by_xpath("//input[@name='{}']".format(name)))>0):
result = self.driver.find_element_by_xpath("//input[@name='{}']".format(name)).get_attribute("value")
elif type == "textArea":
result = self.driver.find_element_by_xpath("//textarea[@name='{}']".format(name)).get_attribute("value")
elif type == "radio":
radiosBtns = self.driver.find_elements_by_name(name)
result = [radio for radio in radiosBtns if radio.get_attribute("checked")=="true"][0].get_attribute("value")
elif type == "checkBox":
result = self.driver.find_element_by_name(name).is_selected()
elif type == "autocomplete":
if name == "service_traps-t[]":
select = Select(self.driver.find_element_by_id("service_traps"))
elif name == "service_categories-t[]":
select = Select(self.driver.find_element_by_id("service_categories"))
else:
select = Select(self.driver.find_element_by_name(name))
result = [[element.text for element in select.options]]
elif type == "select":
select = Select(self.driver.find_element_by_xpath("//select[@name='{}']".format(name)))
result = select.first_selected_option.text
elif type == "click" and name != "submitA":
self.driver.find_element_by_css_selector(name).click()
wait(0.5)
result = True
if name=="service_description":
print("[+] Leyendo {}".format(result))
print("({}, {})".format(name,result))
#pause()
return result
except:
print("[-] Error lectura")
pause()
def set_Input_Value(self,name,type,value):
try:
nname = differents[name] if name in differents else name # nname = new name
if type=="text" and value!="": # value constains text
try:
if not "ARG" in nname:
element = WebDriverWait(self.driver,10).until(lambda d: d.find_element_by_css_selector("input[name='{}']".format(nname)))
else:
element = WebDriverWait(self.driver,1).until(lambda d: d.find_element_by_css_selector("input[name='{}']".format(nname)))
#element = self.driver.find_element_by_css_selector("input[name='{}']".format(nname))
element.send_keys(value)
except:
pass
elif type=="radio": # value contains the value of the one that was previously checked
radioBtns = self.driver.find_elements_by_name(nname)
element = [r for r in radioBtns if r.get_attribute("value")==value][0]
self.driver.execute_script("document.getElementById('{}').click()".format(element.get_attribute("id")))
elif type=="checkBox": # value True if checked
if value:
element = self.driver.find_element_by_name(nname)
self.driver.execute_script("document.getElementById('{}').click()".format(element.get_attribute("id")))
elif type == "select":
if (len(value)>1):
select = WebDriverWait(self.driver,10).until(lambda d: d.find_element_by_name(nname))
parent = self.driver.execute_script("return arguments[0].parentNode;",select)
parent.find_element_by_class_name("select2-container").click()
search_bar = WebDriverWait(self.driver,10).until(lambda d: d.find_elements_by_class_name("select2-container--open"))
search_bar_input = [sb for sb in search_bar if len(sb.find_elements_by_tag_name("input"))>0][0].find_element_by_tag_name("input")
search_bar_input.send_keys(value)
wait(DELAY/2)
search_bar_input.send_keys(Keys.ENTER)
elif type=="autocomplete":
value = value[0]
if nname == "service_traps-t[]":
select = self.driver.find_element_by_id("service_traps")
elif nname == "service_categories-t[]":
select = self.driver.find_element_by_id("service_categories")
else:
select = WebDriverWait(self.driver,10).until(lambda d: d.find_element_by_name(nname))
parent = self.driver.execute_script("return arguments[0].parentNode;",select)
clickable = WebDriverWait(parent, 10).until(lambda d: d.find_element_by_css_selector("span.select2-container"))
clickable.click()
search_bar_input = WebDriverWait(parent,10).until(lambda d: d.find_element_by_css_selector("input.select2-search__field"))
for i in range(0,len(value)):
if(len(value[i])>1):
search_bar_input.send_keys(value[i])
wait(DELAY/2)
search_bar_input.send_keys(Keys.ENTER)
if i <len(value)-1:
wait(DELAY/2)
search_bar_input.send_keys("a")
wait(DELAY/2)
search_bar_input.send_keys(Keys.BACKSPACE*(len(value[i])))
search_bar_input.send_keys(Keys.ESCAPE)
elif type == "click":
if "li#c" in nname:
index = nname[4]
self.driver.execute_script("javascript:montre('{}');".format(index))
elif "submitA" == nname:
self.driver.execute_script("document.getElementsByName('submitA')[0].click();")
wait(DELAY)
else:
self.driver.find_element_by_name(nname)
wait(0.5)
if name=="service_description":
print("[+] P {} I {} Añadiendo {}".format(self.page_number,self.item_offset,value))
return True
except Exception as e:
if not "ARG" in name:
print(e)
return False
else:
return True
def create_Host(self,data):
self.driver.get(self.base+"main.php?p=60101&o=a")
wait(DELAY)
for oinp in HOST_OUTPUTS:
self.set_Input_Value(oinp.name,oinp.type,data[oinp.name])
self.item_offset+=1
def create_Hosts(self,data):
for host in data:
self.create_Host(host)
def get_Host_Information(self,url):
values = {}
self.driver.get(url)
wait(DELAY)
for inp in HOST_INPUTS:
values[inp.name] = self.get_Input_Value(inp.name,inp.type)
return values
def get_Hosts_Information(self):
self.get_Urls()
result = []
for url in self.urls:
result.append(self.get_Host_Information(url))
return result
def create_Service(self,data):
self.driver.get(self.base+"main.php?p=60201&o=a")
wait(DELAY)
iframe = WebDriverWait(self.driver,10).until(lambda d: d.find_element_by_id("main-content"))
self.driver.switch_to.frame(iframe)
for oinp in SERVICE_OUTPUTS:
if not oinp.name in data and oinp.type == "click":
result = self.set_Input_Value(oinp.name,oinp.type,None)
else:
result = self.set_Input_Value(oinp.name,oinp.type,data[oinp.name])
if not result:
print("[-] Fallo en name={}. Página {} item {} Servicio {}".format(oinp.name, self.page_number,self.item_offset,data["service_description"]))
pause()
self.item_offset+=1
def create_Services(self,data):
count = 0
for service in data:
if(count%10 == 0 and count > 0):
print("[{}] {}%".format(count,round(100*count/len(data),2)))
self.create_Service(service)
count += 1
def get_Service_Information(self,url):
values = {}
self.driver.get(url)
wait(DELAY)
for inp in SERVICE_INPUTS:
values[inp.name] = self.get_Input_Value(inp.name,inp.type)
return values
def get_new_Service_Information(self,url):
values = {}
self.driver.get(url.replace('"\'',"").replace('\'"',""))
wait(DELAY)
try:
iframe = WebDriverWait(self.driver,30).until(lambda d: d.find_element_by_id("main-content"))
self.driver.switch_to.frame(iframe)
except:
print("[+] Error iframe")
pause()
iframe = WebDriverWait(self.driver,30).until(lambda d: d.find_element_by_id("main-content"))
self.driver.switch_to.frame(iframe)
for inp in SERVICE_OUTPUTS:
nname = differents[inp.name] if inp.name in differents else inp.name # nname = new name
values[inp.name] = self.get_Input_Value(nname,inp.type)
print(values)
return values
def get_Services_Information(self, urls):
result = []
count = 0
for (host,url) in urls:
if count % 10==0 and count>0:
print("[+] {}%".format(round(100*count/len(urls),2)))
service = self.get_Service_Information(url)
service["host_name"] = host
result.append(service)
count += 1
return result
def get_new_Services_Information(self, urls):
result = []
count = 0
for url in urls:
if count % 10==0 and count>0:
print("[+] {}%".format(round(100*count/len(urls),2)))
service = self.get_new_Service_Information(url)
service["link"] = url
result.append(service)
count += 1
return result
def next_host_page(self):
self.page_number += 1
self.item_offset = 0
self.driver.get(self.base+"01&num={}&limit=30&poller=0&template=0&search=&type=&o=&search_type_service=1&search_type_host=1&hostgroup=0".format(self.page_number))
wait(DELAY)
def next_service_page(self):
self.page_number += 1
self.item_offset = 0
self.driver.get("http://boreal.dipusevilla.es/boreal/main.php?p=60201&num={}&limit=30&poller=&template=0&search=&type=&o=&search_type_service=1&search_type_host=1&status=&hostgroups=0".format(self.page_number))
wait(DELAY)