You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ORPA-pyOpenRPA/Sources/pyOpenRPA/Tools/RobotRDPActive/RDPConnector.py

134 lines
6.3 KiB

#Import parent folder to import current / other packages
from pyOpenRPA.Robot import UIDesktop #Lib to access RDP window
import os #os for process run
import uuid #temp id for Template.rdp
import tempfile #Temporary location
import time
import subprocess
#Connect to RDP session
"""
{
"Host": "", #Host address
"Port": "", #RDP Port
"Login": "", # Login
"Password": "", #Password
"Screen": {
"Resolution":"FullScreen", #"640x480" or "1680x1050" or "FullScreen". If Resolution not exists set full screen
"FlagUseAllMonitors": False, # True or False
"DepthBit":"" #"32" or "24" or "16" or "15"
}
}
"""
def SessionConnect(inRDPSessionConfiguration):
#Run mstsc
from pywinauto.application import Application
lRDPApplication = Application(backend="uia").start("mstsc.exe")
lProcessId = lRDPApplication.process
#Expand the parameter section
UIDesktop.UIOSelector_Get_UIO(
[
5 years ago
{"process": lProcessId, "backend": "uia"},
{"class_name": "ToolbarWindow32"},
{"title": "Показать параметры ", "control_type": "Button"}]
).click()
5 years ago
#Select flag ask login/pass
UIDesktop.UIOSelector_Get_UIO(
[
{"process": lProcessId, "backend": "win32"},
{"title":"Общие"},
{"title":"Учетные данные"},
{"title":"&Всегда запрашивать учетные данные", "class_name":"Button"}]
).check()
#Set host:port
lHostPort=inRDPSessionConfiguration['Host']
if 'Port' in inRDPSessionConfiguration:
lHostPort=f"{lHostPort}:{inRDPSessionConfiguration['Port']}"
5 years ago
UIDesktop.UIOSelector_Get_UIO(
[
{"process": lProcessId, "backend": "uia"},
{"title": "Компьютер:"},
{"title": "Компьютер:", "control_type": "Edit"}]
).set_text(f"{lHostPort}")
#Set user
return
#Add login/ password to the windows credentials to run RDP
def LoginPassSet(inHost,inLogin,inPassword):
#Clear old login/password if it exists
#os.system(f"cmdkey /delete:TERMSRV/{inHost}") #Dont need to delete because new user password will clear the previous creds
#Set login password for host
os.system(f"cmdkey /generic:TERMSRV/{inHost} /user:{inLogin} /pass:{inPassword}")
return None
#Create current .rdp file with settings
#Return (full path to file, session hex)
def RDPConfigurationCreate(inConfiguration):
#RobotRDPActive folder path
lFileFullPath=__file__
lFileFullPath = lFileFullPath.replace("/","\\")
lRobotRDPActiveFolderPath = "\\".join(lFileFullPath.split("\\")[:-1])
#Full path to Template.rdp file
lRDPTemplateFileFullPath = os.path.join(lRobotRDPActiveFolderPath, "Template.rdp")
#Open template file (.rdp encoding is USC-2 LE BOM = UTF-16 LE) http://qaru.site/questions/7156020/python-writing-a-ucs-2-little-endian-utf-16-le-file-with-bom
lRDPTemplateFileContent = open(lRDPTemplateFileFullPath, "r", encoding="utf-16-le").read()
#Prepare host:port
lHostPort=inConfiguration['Host']
if 'Port' in inConfiguration:
lHostPort=f"{lHostPort}:{inConfiguration['Port']}"
#Replace {Width}, {Height}, {BitDepth}, {HostPort}, {Login}
lRDPTemplateFileContent = lRDPTemplateFileContent.replace("{Width}", str(1680))
lRDPTemplateFileContent = lRDPTemplateFileContent.replace("{Height}", str(1050))
lRDPTemplateFileContent = lRDPTemplateFileContent.replace("{BitDepth}", inConfiguration.get('Screen',{}).get("DepthBit","32"))
lRDPTemplateFileContent = lRDPTemplateFileContent.replace("{HostPort}", lHostPort)
lRDPTemplateFileContent = lRDPTemplateFileContent.replace("{Login}", inConfiguration['Login'])
#Save template to temp file
lRDPCurrentFileFullPath = os.path.join(tempfile.gettempdir(), f"{uuid.uuid4().hex}.rdp")
open(lRDPCurrentFileFullPath, "w", encoding="utf-16-le").write(lRDPTemplateFileContent)
#Return .rdp full path
return (lRDPCurrentFileFullPath, (lRDPCurrentFileFullPath.split("\\")[-1])[0:-4])
#RDPSessionStart
def RDPSessionStart(inRDPFilePath):
#run rdp session
lItemArgs = [inRDPFilePath]
subprocess.Popen(lItemArgs, shell=True)
#Wait for UAC unknown publisher exists
lRDPFileName = (inRDPFilePath.split("\\")[-1])[0:-4]
lWaitResult = UIDesktop.UIOSelectorsSecs_WaitAppear_List(
[
[{"title": "Подключение к удаленному рабочему столу", "class_name": "#32770", "backend": "win32"},
{"title": "Боль&ше не выводить запрос о подключениях к этому компьютеру", "friendly_class_name": "CheckBox"}],
[{"title_re": f"{lRDPFileName} — .*",
"class_name": "TscShellContainerClass", "backend": "win32"}]
],
60
)
#Click if 0 is appear
if 0 in lWaitResult:
#Check the box do not retry
UIDesktop.UIOSelector_Get_UIO([{"title": "Подключение к удаленному рабочему столу", "backend": "win32"},
{"title": "Боль&ше не выводить запрос о подключениях к этому компьютеру", "friendly_class_name": "CheckBox"}]).check()
#Go to connection
UIDesktop.UIOSelector_Get_UIO([{"title": "Подключение к удаленному рабочему столу", "backend": "win32"},
{"title":"Подкл&ючить", "class_name":"Button"}]).click()
lWaitResult = UIDesktop.UIOSelectorsSecs_WaitAppear_List(
[
[{"title_re": f"{lRDPFileName} — .*",
"class_name": "TscShellContainerClass", "backend": "win32"}]
],
60
)
#Prepare little window
SessionLittleScreen(lRDPFileName)
return None
#Set fullscreen for app
def SessionFullScreen(inSessionHex):
#Prepare little window
lRDPWindow = UIDesktop.UIOSelector_Get_UIO([{"title_re": f"{lRDPFileName} — .*", "backend": "win32"}])
lRDPWindow.maximize()
return None
#Set Little window of the session
def SessionLittleScreen(inSessionHex):
#Prepare little window
lRDPWindow = UIDesktop.UIOSelector_Get_UIO([{"title_re": f"{lRDPFileName} — .*", "backend": "win32"}])
lRDPWindow.restore()
lRDPWindow.move_window(10,10,550,100)
return None