Last active
December 13, 2015 16:39
-
-
Save bdargan/4942070 to your computer and use it in GitHub Desktop.
selenium webdriver for automating some qradar tasks, like updating assets or autoatically extracting report data.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
from selenium import webdriver | |
import selenium | |
from selenium.common.exceptions import TimeoutException | |
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0 | |
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0 | |
from selenium.webdriver.common.by import By | |
import collections | |
from time import sleep | |
import getpass | |
def qradar_login(url, username, password): | |
d.get(url) | |
d.find_element_by_name("j_username").send_keys(username) | |
d.find_element_by_name("j_password").send_keys(password) | |
d.find_elements_by_xpath('//form')[0].submit() | |
def consume_alerts(): | |
try: | |
for i in range(10): | |
d.switch_to_alert().accept() | |
except: | |
pass | |
def find_asset(key,value): | |
d.find_element_by_id('TAB_ASSETS').click() | |
# PAGE_ASSETS not really wokring | |
d.switch_to_frame(d.find_elements_by_tag_name("iframe")[4]) | |
d.switch_to_frame("rightPane") | |
try: | |
d.find_element_by_name(key).send_keys(value) | |
d.find_element_by_id("searchButton1").click() | |
# just pick the first clicable row | |
WebDriverWait(d, 3).until(EC.element_to_be_clickable((By.TAG_NAME,"TR"))) | |
d.find_elements_by_tag_name('tr')[1].click() | |
d.execute(selenium.webdriver.common.action_chains.Command.DOUBLE_CLICK, {}) | |
#should verify we have the detail page we want | |
# doesn't work, different input names, searchIp => IpAddress | |
# asset = d.find_element_by_name(key) | |
# if (asset.get_attribute('value') == value): | |
# print "we have the right record" | |
# return we | |
except Exception, e: | |
print e | |
def update_asset(key, value, props): | |
we = find_asset(key,value) | |
for k,v in props.items(): | |
we = d.find_element_by_name(k) | |
if we: | |
we.clear() | |
# we.send_keys(Keys.CONTROL +'a'+Keys.NULL), | |
we.send_keys(v) | |
print we.get_attribute('value') | |
d.find_element_by_name('saveButton').click() | |
def inspect(wel): | |
if isinstance(wel, collections.Iterable): | |
for e in wel: | |
inspect(e) | |
else: | |
print "%s, %s, %s" % (wel.get_attribute('id'), wel.get_attribute('name'), wel.get_attribute('value')) | |
def list_iframes(): | |
inspect(d.find_elements_by_tag_name('iframe')) | |
def list_inputs(): | |
inspect(d.find_elements_by_tag_name('input')) | |
def rfindall(pattern, flags=0): | |
p = re.compile (pattern,flags) | |
return p.findall(d.page_source) | |
try: | |
d = webdriver.Chrome() | |
# d.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS); | |
# interesting, ffox gets alerts, chrome, no | |
qradar_login("https://qradar/console/qradar/jsp/QRadar.jsp", getpass.getuser(), getpass.getpass("password please:")) | |
sleep(5) | |
consume_alerts() | |
update_asset("searchIp", "127.0.0.1", {'assetName':'localhost', 'assetDescription':'Theres no place like 127.0.0.1'}) | |
finally: | |
d.quit() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment