scripts/afu/mi2.py

117 lines
3.8 KiB
Python
Executable File

#!/usr/bin/env python3
# Thomas Kuschel, following with more ....
# changes to python-seleniumbase 4.25/4.27.5-1 python-selenium 4.21.0-1 (installed)
# changes:
# #001 The Tab URL at phpmyadmin has changed from server_export to server/export
# #002 The the visibility of the site is critically, so we have to put in the line option.add_argument('window-size=1200x600')
import argparse
#import datetime
import os
import time
#import re
import sys
#import grab
# yay extra/python-pip
# pip install selenium
# ! pip install webdriver-manager
# yay geckodriver (for Firefox browser)
import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
__version__ = '2.0.1'
def call_website(url,username,password,server,verbose=0,interactive=False):
# using Chrome to access web
# driver = webdriver.Firefox(executable_path=r'/usr/bin/geckodriver')
if(interactive):
print('Interactive')
driver = webdriver.Chrome()
else: # do a headless script base work (default)
print('Headless Script')
service = Service(executable_path='/usr/bin/chromedriver')
option = webdriver.ChromeOptions()
option.add_argument('headless')
option.add_argument('window-size=1200x600')
driver = webdriver.Chrome(options=option, service=service)
driver.get(url)
assert "phpMyAdmin" in driver.title
print('Driver-Title: ' + driver.title)
elem = driver.find_element(By.ID, "input_username")
elem.clear()
elem.send_keys(username)
elem = driver.find_element(By.ID, "input_password")
elem.clear()
elem.send_keys(password)
elem = driver.find_element(By.ID, "select_server")
from selenium.webdriver.support.ui import Select
select = Select(driver.find_element(By.ID, 'select_server'))
select.select_by_visible_text(server)
driver.find_element(By.ID, "input_go").click()
time.sleep(1)
#driver.find_element(By.LINK_TEXT, "Export").click()
#driver.find_element_by_xpath('//a[contains(@href,"server_export")]').click()
#print(driver.page_source.encode("utf-8"))
element = driver.find_element(By.XPATH,'//a[contains(@href,"server/export")]')
print("Element is visible? " + str(element.is_displayed()))
element.click()
time.sleep(5)
driver.find_element(By.ID, "buttonGo").click()
time.sleep(1)
print(driver.current_window_handle)
#w = driver.window_handles[1]
#driver.switch_to.window(child)
#driver.fullscreen_window()
try:
# WebDriverWait(browser, 3).until(EC.alert_is_present(),
# 'Timed out waiting for PA creation ' +
# 'confirmation popup to appear.')
alert = driver.switch_to.alert
alert.accept()
print("alert accepted")
except:
print("no alert")
time.sleep(20)
driver.close()
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Automates downloading the SQL dump via phpMyAdmin',
epilog='''
Written by Thomas Kuschel,
inspired by Christoph Haunschmidt et al.,
Version {}
'''.format(__version__))
parser.add_argument('--interactive', '-i', action='store_true', default=False)
parser.add_argument('--verbose', '-v', action='count', default=0)
parser.add_argument('--version', action='version', version='{} {}'.format(os.path.split(__file__)[1],__version__))
# parser.parse_args(['--version'])
parser.add_argument('url', metavar='URL', help='phpMyAdmin login page URL')
parser.add_argument('--username', '-u', default='', help='phpMyAdmin login username', required=False)
parser.add_argument('--password', '-p', default='', help='phpMyAdmin login password', required=False)
parser.add_argument('--server', '-s', default='kreios', required=False)
args = parser.parse_args()
try:
print(f'Try calling {args.url}')
filename = call_website(**vars(args))
sys.exit(0)
except Exception as e:
print('Error: {}'.format(e), file=sys.stderr)
sys.exit(1)
print('Verbose level: {}'.format(args.verbose))