|
| 1 | +#!/bin/env python3 |
| 2 | +from pyvirtualdisplay import Display |
| 3 | +from selenium import webdriver |
| 4 | +import time |
| 5 | +import os |
| 6 | +import logging |
| 7 | + |
| 8 | +class Glp_SangFor: |
| 9 | + def __init__(self,logger): |
| 10 | + self.logger = logger |
| 11 | + self.logger.info("--------------start log----------------") |
| 12 | + self.display = Display(visible=0, size=(800, 600)) |
| 13 | + self.display.start() |
| 14 | + self.browser = webdriver.Firefox() |
| 15 | + self.logger.info("start browser successfuly") |
| 16 | + self.sangfor_url = "深信服url" |
| 17 | + self.username = '深信服用户名' |
| 18 | + self.password = '深信服密码' |
| 19 | + |
| 20 | + def login(self): |
| 21 | + self.browser.get(self.sangfor_url) |
| 22 | + self.browser.implicitly_wait(5) |
| 23 | + self.browser.find_element_by_name('user').send_keys(self.username) |
| 24 | + self.browser.find_element_by_name('password').send_keys(self.password) |
| 25 | + self.browser.find_element_by_class_name('buttons').click() |
| 26 | + self.browser.implicitly_wait(5) |
| 27 | + self.logger.info("loggin sangfor successfuly") |
| 28 | + |
| 29 | + def client_reboot(self): |
| 30 | + self.browser.find_element_by_id("ext-gen111").click() |
| 31 | + print(self.browser.find_element_by_id("ext-gen111").text) |
| 32 | + self.browser.implicitly_wait(15) |
| 33 | + time.sleep(60) |
| 34 | + self.logger.info("switch mainiframe start") |
| 35 | + try: |
| 36 | + print(self.browser.find_element_by_link_text("重启/重启服务/关机").text) |
| 37 | + self.browser.find_element_by_link_text("重启/重启服务/关机").click() |
| 38 | + self.browser.implicitly_wait(3) |
| 39 | + self.browser.switch_to_frame("mainiframe") |
| 40 | + self.browser.implicitly_wait(8) |
| 41 | + time.sleep(10) |
| 42 | + self.browser.find_element_by_xpath("//button[@id='ext-gen19']").click() |
| 43 | + print(self.browser.find_element_by_xpath("//button[@id='ext-gen19']").text) |
| 44 | + self.browser.implicitly_wait(10) |
| 45 | + #self.browser.find_element_by_xpath("//button[@id='ext-gen42']").click() |
| 46 | + print(self.browser.find_element_by_xpath("//button[@id='ext-gen42']").text) |
| 47 | + except Exception as e: |
| 48 | + self.logger.exception("reboot successful") |
| 49 | + return 1 |
| 50 | + self.browser.close() |
| 51 | + self.logger.info("browser close successful") |
| 52 | + self.logger.info("--------------end log----------------") |
| 53 | + return 0 |
| 54 | + |
| 55 | +class Glp_Log: |
| 56 | + def __init__(self,filename): |
| 57 | + self.filename = filename |
| 58 | + def createDir(self): |
| 59 | + _LOGDIR = os.path.join(os.path.dirname(__file__), 'publiclog') |
| 60 | + print(_LOGDIR) |
| 61 | + _TIME = time.strftime('%Y-%m-%d', time.gmtime()) + '-' |
| 62 | + _LOGNAME = _TIME + self.filename |
| 63 | + print(_LOGNAME) |
| 64 | + LOGFILENAME = os.path.join(_LOGDIR, _LOGNAME) |
| 65 | + print(LOGFILENAME) |
| 66 | + if not os.path.exists(_LOGDIR): |
| 67 | + os.mkdir(_LOGDIR) |
| 68 | + return LOGFILENAME |
| 69 | + print(LOGFILENAME) |
| 70 | + |
| 71 | + def createlogger(self,logfilename): |
| 72 | + logger= logging.getLogger() |
| 73 | + logger.setLevel(logging.INFO) |
| 74 | + handler = logging.FileHandler(logfilename) |
| 75 | + handler.setLevel(logging.INFO) |
| 76 | + formater = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') |
| 77 | + handler.setFormatter(formater) |
| 78 | + logger.addHandler(handler) |
| 79 | + return logger |
| 80 | + |
| 81 | +if __name__ == '__main__': |
| 82 | + os.system("pkill firefox") |
| 83 | + os.system("pkill Xvfb") |
| 84 | + glploger = Glp_Log('public-vpn.log') |
| 85 | + logfilename = glploger.createDir() |
| 86 | + logger = glploger.createlogger(logfilename) |
| 87 | + |
| 88 | + sangfor_oper = Glp_SangFor(logger) |
| 89 | + sangfor_oper.login() |
| 90 | + sangfor_oper.client_reboot() |
0 commit comments