Jump to content

[Automation]Start Oracle VM + WinSCP and Auth + Client startup


Recommended Posts

  • Premium

Hello,

Some boring clicking automated that I use, feel free to improve it, add waits, w/e, w/e

import time, os
from pywinauto import Desktop, Application
import bcolors
from pywinauto.keyboard import send_keys
import psutil


class mySession:
    app = Application(backend='uia')

    def __init__(self, vm_Name='', ssh_Name='', cliente_exe_Path='', winSCP_path='', vbox_exe_path=''):
        self.vm_Name = vm_Name
        self.ssh_Name = ssh_Name
        self.cliente_exe_Path = cliente_exe_Path
        pathList = cliente_exe_Path.split('\\')
        self.client_path = '/'.join(pathList[:-1])
        self.exe_name = pathList[-1]
        self.vbox_exe_path = vbox_exe_path
        self.winSCP_path = winSCP_path

    def openVM(self, waitServer):
        self.killProcess("VirtualBox.exe")
        self.app.start(self.vbox_exe_path)
        self.app = Application(backend='uia').connect(title='Oracle VM VirtualBox Manager', timeout=10)
        # self.app.Dialog.print_control_identifiers()
        self.app.OracleVMVirtualBoxManager.child_window(title=self.vm_Name, control_type="ListItem").wrapper_object().click_input(button='left', double=True)
        # self.app = Application(backend='uia').connect(title=self.whoAmI('Starting')[0], timeout=10)
        time.sleep(waitServer)
        print(f"\t{bcolors.OK}{bcolors.OKMSG}VM is ready{bcolors.ENDC}")

    def openWinSCP(self):
        self.killProcess("WinSCP.exe")
        self.app.start(self.winSCP_path)
        self.app = Application(backend='uia').connect(title=self.whoAmI(keyword='wins')[0], timeout=40)
        time.sleep(1)

        if self.app.Dialog.child_window(title=self.ssh_Name + "      ", control_type="TabItem").exists():
            self.app.Dialog.child_window(title="Reconnect Session", control_type="Button").wrapper_object().click_input(
                button='left')
        else:
            self.app.Dialog.child_window(title="New Session", control_type="TabItem").wrapper_object().click_input(button='left')

        auth_win = self.app.Dialog.child_window(title=self.ssh_Name, control_type="TreeItem")
        auth_win.wait('visible', timeout=50)
        auth_win.wrapper_object().click_input(button='left', double=True)

        input_pass = self.app.Dialog.child_window(control_type="Edit")
        input_pass.wait('visible', timeout=50)
        input_pass.wrapper_object().type_keys("dev")

        self.app.Dialog.child_window(title="OK", control_type="Button").wrapper_object().click_input(button='left')
        time.sleep(2)
        print(f"\t{bcolors.OK}{bcolors.OKMSG}WinScp is ready{bcolors.ENDC}")

    def startClient(self, do_login=True):
        os.chdir(self.client_path)
        os.startfile(self.client_path + "\\" + self.exe_name)
        # self.app = Application(backend='uia').connect(title='Metin2', timeout=10)

    def whoAmI(self, keyword='', retryLimit=2, isFull=False):
        while retryLimit:
            windows = Desktop(backend="uia").windows()
            windowsList = [w.window_text() for w in windows]
            resultList = []
            for i in windowsList:
                if keyword.lower() in i.lower():
                    resultList.append(i)

            if not len(resultList):
                time.sleep(0.5)
                self.whoAmI(keyword=keyword, retryLimit=retryLimit-1, isFull=isFull)
            else:
                # print('Matches: ', resultList)
                if not isFull:
                    return resultList
                else:
                    return windowsList

    def killProcess(self, proc_nmame, kill_=True):
        if kill_:
            for proc in psutil.process_iter():
                # check whether the process name matches
                if proc.name() == proc_nmame:
                    proc.kill()
            time.sleep(0.5)

if __name__ == "__main__":
    print(f"{bcolors.OK}#########[START]#########{bcolors.ENDC}")
    #Here you have to give the name of ur Virtual Machine(vm_Name), winSCP ssh connetion name(ssh_Name), and ur client executable path
    controller = mySession(vm_Name="11.3_5.5_v4",
                           ssh_Name="alpha_server",
                           cliente_exe_Path=r"C:\Users\itsas\Desktop\M2\versionControl\client\Metin2\metin2_launcher.exe",
                           vbox_exe_path='"C:\Program Files\Oracle\VirtualBox\VirtualBox.exe"',
                           winSCP_path="C:\Program Files (x86)\WinSCP\WinSCP.exe")
                            
    controller.openVM(waitServer=15)
    controller.openWinSCP()
    controller.startClient()
    print(f"{bcolors.OK}#########[END]#########{bcolors.ENDC}")

 

Prereq:

Python 3.9

Libs: pywinauto, psutil, bcolors

Note: The script does kill existing processes of WinSCP and oracle VM, you can disable it, if u feel the need, search for killProcess method and set default param to False

 

Edit: Parametrized paths for vbox, winSCP also :)

Edited by astroNOT
  • Love 1
Link to comment
Share on other sites

Announcements



×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.