# Update the RDP active - improove work of the RDP cmd sent + feedback

dev-linux
Ivan Maslov 4 years ago
parent 4dde6d1501
commit f4ed1ff981

@ -15,10 +15,10 @@ gRecoveryWindowRDPRetryCountInt = 3 # Retry iteration count is RDP window is not
gRecoveryWindowRDPRetryIntervalSecInt = 3 # Retry interval for reconnect gRecoveryWindowRDPRetryIntervalSecInt = 3 # Retry interval for reconnect
gRecoveryWindowRUNRetryCountInt = 3 # Retry iteration count is RUN window is not responsible gRecoveryWindowRUNRetryCountInt = 3 # Retry iteration count is RUN window is not responsible
gRecoveryWindowRUNRetryIntervalSecInt = 3 # Retry interval for reconnect gRecoveryWindowRUNRetryIntervalSecInt = 3 # Retry interval for retry
gRecoveryCMDResponsibleRetryCountInt = 3 # Retry iteration count is CMD is not responsible gRecoveryCMDResponsibleRetryCountInt = 3 # Retry iteration count is CMD is not responsible
gRecoveryCMDResponsibleRetryIntervalSecInt = 3 # Retry interval for reconnect gRecoveryCMDResponsibleRetryIntervalSecInt = 3 # Retry interval for retry
#Connect to RDP session #Connect to RDP session
""" """
@ -159,23 +159,26 @@ def SessionScreenFull(inSessionHex, inGSettings = None):
lRDPConfigurationItem = None lRDPConfigurationItem = None
lL = None lL = None
if inGSettings: # Get the values from gSettings if inGSettings: # Get the values from gSettings
lRDPConfigurationItem = inGSettings["RobotRDPActive"]["RDPList"][inRDPSessionKeyStr] # Get the RDP configuration item lRDPConfigurationItem = inGSettings["RobotRDPActive"]["RDPList"][inSessionHex] # Get the RDP configuration item
lL = inGSettings["Logger"] # Get the logger instance lL = inGSettings["Logger"] # Get the logger instance
while lWindowRDPRetryIterator<gRecoveryWindowRDPRetryCountInt: # Loop iteration to connect to RDP while lWindowRDPRetryIterator<gRecoveryWindowRDPRetryCountInt: # Loop iteration to connect to RDP
try: # Try to get RDP window try: # Try to get RDP window
lRDPWindow = UIDesktop.UIOSelector_Get_UIO([{"title_re": f"{inSessionHex}.*", "backend": "win32"}]) lRDPWindow = UIDesktop.UIOSelector_Get_UIO([{"title_re": f"{inSessionHex}.*", "backend": "win32"}])
lWindowRDPRetryIterator = gRecoveryWindowRDPRetryCountInt # Set last iterator to turn off the loop lWindowRDPRetryIterator = gRecoveryWindowRDPRetryCountInt # Set last iterator to turn off the loop
except Exception as e: # RDP window is not exist - try to reconnect except Exception as e: # RDP window is not exist - try to reconnect
if inLogger: inLogger.warning(f"RDP::SessionScreenFull: RDP window is not exist - try sleep {gRecoveryWindowRDPRetryIntervalSecInt}[s.] and then to reconnect. SessionHex: {inSessionHex}") if lL: lL.warning(f"RDP::SessionScreenFull: RDP window is not exist - try sleep {gRecoveryWindowRDPRetryIntervalSecInt}[s.] and then to reconnect. SessionHex: {inSessionHex}. Current retry iterator is {lWindowRDPRetryIterator}")
time.sleep(gRecoveryWindowRDPRetryIntervalSecInt) # Before try to reconnect sleep
if lRDPConfigurationItem: if lRDPConfigurationItem:
time.sleep(gRecoveryWindowRDPRetryIntervalSecInt) # Before try to reconnect sleep
# Try to reconnect the RDP # Try to reconnect the RDP
Connector.SessionClose(inSessionHexStr=lSessionHex) # Close the SessionClose(inSessionHexStr=inSessionHex) # Close the
Connector.Session(lRDPConfigurationItem) # Create RDP session Session(lRDPConfigurationItem) # Create RDP session
lWindowRDPRetryIterator = lWindowRDPRetryIterator + 1 # increase the iterator lWindowRDPRetryIterator = lWindowRDPRetryIterator + 1 # increase the iterator
if lWindowRDPRetryIterator >= gRecoveryWindowRDPRetryCountInt: # Raise the error if retry count is over
if lL: lL.warning(f"RDP::SessionScreenFull: Retry count is over. Raise the error. SessionHex: {inSessionHex}.") # Log the info
raise ConnectorExceptions.SessionWindowNotExistError() # raise the error
else: else:
if inLogger: inLogger.warning(f"RDP::SessionScreenFull: Has no RDP configuration item - don't reconnect. Raise the error. SessionHex: {inSessionHex}") # Log the info if lL: lL.warning(f"RDP::SessionScreenFull: Has no RDP configuration item - don't reconnect. Raise the error. SessionHex: {inSessionHex}") # Log the info
raise ConnectorExceptions.SessionWindowNotExistError() # raise the errors raise ConnectorExceptions.SessionWindowNotExistError() # raise the error
# RDP window has been detected - go to set focus and maximize # RDP window has been detected - go to set focus and maximize
lRDPWindow.set_focus() lRDPWindow.set_focus()
lRDPWindow.maximize() lRDPWindow.maximize()
@ -231,13 +234,15 @@ def SessionClose(inSessionHexStr):
# } # }
# example Connector.SessionCMDRun("4d1e48f3ff6c45cc810ea25d8adbeb50","start notepad", "RUN") # example Connector.SessionCMDRun("4d1e48f3ff6c45cc810ea25d8adbeb50","start notepad", "RUN")
def SessionCMDRun(inSessionHex,inCMDCommandStr = "echo 1", inModeStr="CROSSCHECK", inClipboardTimeoutSec = 5, inGSettings = None): def SessionCMDRun(inSessionHex,inCMDCommandStr = "echo 1", inModeStr="CROSSCHECK", inClipboardTimeoutSec = 5, inGSettings = None):
lL = None # Init the logger
if inGSettings: # If gSettings is exist
lL = inGSettings["Logger"] # get the logger
# Init the result dict # Init the result dict
lResult = {"OutStr": None,"IsResponsibleBool":True} lResult = {"OutStr": None,"IsResponsibleBool":True}
# Enter full screen mode SessionScreenFull(inSessionHex, inGSettings = inGSettings) # Enter full screen mode with recovery scenario
SessionScreenFull(inSessionHex, inGSettings = inGSettings)
time.sleep(2) time.sleep(2)
# Run CMD operations # Run CMD operations
lResult = SystemCMDRun(inCMDCommandStr = inCMDCommandStr, inModeStr = inModeStr, inClipboardTimeoutSec = inClipboardTimeoutSec) lResult = SystemCMDRun(inCMDCommandStr = inCMDCommandStr, inModeStr = inModeStr, inClipboardTimeoutSec = inClipboardTimeoutSec, inLogger=lL)
# Exit fullscreen mode # Exit fullscreen mode
SessionScreenSize_X_Y_W_H(inSessionHex=inSessionHex, inXInt=10, inYInt=10, inWInt=550, SessionScreenSize_X_Y_W_H(inSessionHex=inSessionHex, inXInt=10, inYInt=10, inWInt=550,
inHInt=350) # Prepare little window inHInt=350) # Prepare little window
@ -289,43 +294,81 @@ def SessionIsMinimizedScreen(inSessionHexStr):
# "IsResponsibleBool": True|False # Flag is RDP is responsible - works only when inModeStr = CROSSCHECK # "IsResponsibleBool": True|False # Flag is RDP is responsible - works only when inModeStr = CROSSCHECK
# } # }
# example Connector.SessionCMDRun("4d1e48f3ff6c45cc810ea25d8adbeb50","start notepad", "RUN") # example Connector.SessionCMDRun("4d1e48f3ff6c45cc810ea25d8adbeb50","start notepad", "RUN")
def SystemCMDRun(inCMDCommandStr = "echo 1", inModeStr="CROSSCHECK", inClipboardTimeoutSec = 5): def SystemCMDRun(inCMDCommandStr = "echo 1", inModeStr="CROSSCHECK", inClipboardTimeoutSec = 5, inLogger = None):
# Set random text to clipboard (for check purposes that clipboard text has been changed) lL = inLogger # Alias for logger
lClipboardTextOld = str(random.randrange(999,9999999)) lResult = {"OutStr": None,"IsResponsibleBool":True} # Init the result dict
lClipboardTextOld = str(random.randrange(999,9999999)) # Set random text to clipboard (for check purposes that clipboard text has been changed)
Clipboard.TextSet(lClipboardTextOld) Clipboard.TextSet(lClipboardTextOld)
# Init the result dict
lResult = {"OutStr": None,"IsResponsibleBool":True}
lCrosscheckKeyStr = str(random.randrange(999,9999999)) lCrosscheckKeyStr = str(random.randrange(999,9999999))
lCMDPostFixStr = "" # Case default "RUN" lRecoveryCMDResponsibleRetryIteratorInt = 0 # Init the retry iterator
if inModeStr == "CROSSCHECK": while lRecoveryCMDResponsibleRetryIteratorInt<gRecoveryCMDResponsibleRetryCountInt: # loop for retry
lCMDPostFixStr = f"| echo {lCrosscheckKeyStr} | clip" # # # # # # # # # # # # # OPEN WINDOW RUN # # # # # # # # # # # # # # #
elif inModeStr == "LISTEN": lRecoveryWindowRUNRetryIteratorInt = 0 # Init the retry iterator
lCMDPostFixStr = f"| clip" while lRecoveryWindowRUNRetryIteratorInt<gRecoveryWindowRUNRetryCountInt: # loop for retry
keyboard.press_and_release('win+r') lCMDPostFixStr = "" # Case default "RUN"
time.sleep(1) if inModeStr == "CROSSCHECK":
# Remove old text lCMDPostFixStr = f"| echo {lCrosscheckKeyStr} | clip"
keyboard.press_and_release("ctrl+a") elif inModeStr == "LISTEN":
keyboard.press_and_release("backspace") lCMDPostFixStr = f"| clip"
# Write new text keyboard.press_and_release('win+r')
keyboard.write(f"cmd /c {inCMDCommandStr} {lCMDPostFixStr}") time.sleep(1) # Wait for RUN window will appear
time.sleep(1) keyboard.press_and_release("ctrl+a") # Select all
# TODo cross check from clipboard keyboard.press_and_release("backspace") # Delete selected all
keyboard.press_and_release('enter') lInputStr = f"cmd /c {inCMDCommandStr} {lCMDPostFixStr}" # Generate the output string for RUN window
# Get OutStr (Case CROSSCHECK and LISTEN) keyboard.write(lInputStr) # Write new text
if inModeStr == "CROSSCHECK" or inModeStr == "LISTEN": keyboard.press_and_release("ctrl+a") # Select all
lClipboardWaitTimeStartSec = time.time() keyboard.press_and_release("ctrl+c") # COPY in clipboard to check
lResult["OutStr"] = Clipboard.TextGet() # Get text from clipboard #time.sleep(2 + 2 * lRecoveryWindowRUNRetryIteratorInt) # Wait when data will be copied UPD - no need - wait is below
while lResult["OutStr"] == lClipboardTextOld and (time.time() - lClipboardWaitTimeStartSec) <= inClipboardTimeoutSec: # Check the clipboard
lClipboardWaitTimeStartSec = time.time()
lClipboardStr = Clipboard.TextGet() # Get text from clipboard
while lClipboardStr != lInputStr and (time.time() - lClipboardWaitTimeStartSec) <= inClipboardTimeoutSec:
lClipboardStr = Clipboard.TextGet() # Get text from clipboard
time.sleep(0.5) # wait some time for the next operation
if lClipboardStr == lInputStr: # Cross check the clipboard data and input string
lRecoveryWindowRUNRetryIteratorInt = gRecoveryWindowRUNRetryCountInt # Set final count to block the loop
else: # Failed - wait and retry
if lL: lL.warning(f"RDP::SystemCMDRun: Window run doesn't appear. Wait for {gRecoveryWindowRUNRetryIntervalSecInt}[s.] and retry. Current retry iterator is {lRecoveryWindowRUNRetryIteratorInt}. CMD Str: {lInputStr}") # Log the error
lRecoveryWindowRUNRetryIteratorInt = lRecoveryWindowRUNRetryIteratorInt + 1 # Increment the iterator
if lRecoveryWindowRUNRetryIteratorInt == gRecoveryWindowRUNRetryCountInt:
if lL: lL.warning(f"RDP::SystemCMDRun: Retry count is over. Raise the error. CMD Str: {lInputStr}") # Log the error
raise ConnectorExceptions.RUNExistError() # Raise the error
time.sleep(gRecoveryWindowRUNRetryIntervalSecInt) # wait for some seconds before new iteration
# # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # RUN CMD # # # # # # # # # # # # # # #
keyboard.press_and_release('enter') # Execute CMD
if inModeStr == "CROSSCHECK" or inModeStr == "LISTEN": # Get OutStr (Case CROSSCHECK and LISTEN)
lClipboardWaitTimeStartSec = time.time()
lResult["OutStr"] = Clipboard.TextGet() # Get text from clipboard lResult["OutStr"] = Clipboard.TextGet() # Get text from clipboard
time.sleep(0.5) # wait some time for the next operation while lResult["OutStr"] == lClipboardTextOld and (time.time() - lClipboardWaitTimeStartSec) <= inClipboardTimeoutSec:
# Do crosscheck lResult["OutStr"] = Clipboard.TextGet() # Get text from clipboard
if inModeStr == "CROSSCHECK": time.sleep(0.5) # wait some time for the next operation
if lResult["OutStr"] == f"{lCrosscheckKeyStr} \r\n\x00\x00\x00\x00\x00": if lResult["OutStr"] == lClipboardTextOld: # If value hasn't been changed - retry send
lResult["IsResponsibleBool"] = True lRecoveryCMDResponsibleRetryIteratorInt = lRecoveryCMDResponsibleRetryIteratorInt + 1 # increment the iterator
else: if lL: lL.warning(f"RDP::SystemCMDRun: CMD command doesn't been executed (no changes in clipboard data). Wait for {gRecoveryCMDResponsibleRetryIntervalSecInt}[s.] and retry from start window RUN. Current retry iterator is {lRecoveryCMDResponsibleRetryIteratorInt}. CMD Str: {lInputStr}, Clipboard data: {lResult['OutStr']}") # Log the error
lResult["IsResponsibleBool"] = False if lRecoveryCMDResponsibleRetryIteratorInt >= gRecoveryCMDResponsibleRetryCountInt: # raise the error if retry count is exceeded
# return the result if lL: lL.warning(f"RDP::SystemCMDRun: Retry count is over. Raise the error.") # Log the error
return lResult raise ConnectorExceptions.CMDResponsibleError() # Raise the error
time.sleep(gRecoveryCMDResponsibleRetryIntervalSecInt) # wait for some seconds before new iteration
else: # Data was recieved - do crosscheck
if inModeStr == "CROSSCHECK":
if lResult["OutStr"] == f"{lCrosscheckKeyStr} \r\n\x00\x00\x00\x00\x00": # it is ok - do futher check
lResult["IsResponsibleBool"] = True
lRecoveryCMDResponsibleRetryIteratorInt = gRecoveryCMDResponsibleRetryCountInt # turn off the iterator
else:
lResult["IsResponsibleBool"] = False
lRecoveryCMDResponsibleRetryIteratorInt = lRecoveryCMDResponsibleRetryIteratorInt + 1 # increment the iterator
if lL: lL.warning(f"RDP::SystemCMDRun: CMD command doesn't been executed (wrong clipboard data). Wait for {gRecoveryCMDResponsibleRetryIntervalSecInt}[s.] and retry from start window RUN. Current retry iterator is {lRecoveryCMDResponsibleRetryIteratorInt}. CMD Str: {lInputStr}, Clipboard data: {lResult['OutStr']}") # Log the error
if lRecoveryCMDResponsibleRetryIteratorInt >= gRecoveryCMDResponsibleRetryCountInt: # raise the error if retry count is exceeded
if lL: lL.warning(f"RDP::SystemCMDRun: Retry count is over. Raise the error.") # Log the error
raise ConnectorExceptions.CMDResponsibleError() # Raise the error
time.sleep(gRecoveryCMDResponsibleRetryIntervalSecInt) # wait for some seconds before new iteration
else: # clipboard data has been changed but mode is not crosscheck - return success from function
lRecoveryCMDResponsibleRetryIteratorInt = gRecoveryCMDResponsibleRetryCountInt # turn off the iterator
else: # Success - no cross validation is aaplicable
lRecoveryCMDResponsibleRetryIteratorInt = gRecoveryCMDResponsibleRetryCountInt # turn off the iterator
# # # # # # # # # # # # # # # # # # # # # # # # # # # #
return lResult # return the result
# Check if current RDP is responsible # Check if current RDP is responsible
def SystemRDPIsResponsible(): def SystemRDPIsResponsible():
return SystemCMDRun(inCMDCommandStr = "echo 1", inModeStr="CROSSCHECK")["IsResponsibleBool"] return SystemCMDRun(inCMDCommandStr = "echo 1", inModeStr="CROSSCHECK")["IsResponsibleBool"]

@ -9,7 +9,7 @@ def RobotRDPActive(inGSettings):
# inGSettings = { # inGSettings = {
# ... "RobotRDPActive": {} ... # ... "RobotRDPActive": {} ...
# } # }
lL = gSettingsDict["Logger"] #Logger alias lL = inGSettings["Logger"] #Logger alias
Processor.gSettings = inGSettings # Set gSettings in processor module Processor.gSettings = inGSettings # Set gSettings in processor module
mGSettingsRDPActiveDict = inGSettings["RobotRDPActive"] # Get configuration from global dict settings mGSettingsRDPActiveDict = inGSettings["RobotRDPActive"] # Get configuration from global dict settings
# Global error handler # Global error handler
@ -95,8 +95,7 @@ def RobotRDPActive(inGSettings):
# Check all RDP window and minimize it # Check all RDP window and minimize it
for lRDPSessionKeyStrItem in inGlobalDict["RDPList"]: for lRDPSessionKeyStrItem in inGlobalDict["RDPList"]:
lRDPConfigurationDictItem = inGlobalDict["RDPList"][lRDPSessionKeyStrItem] lRDPConfigurationDictItem = inGlobalDict["RDPList"][lRDPSessionKeyStrItem]
if Connector.SessionIsFullScreen(inSessionHexStr=lRDPConfigurationDictItem["SessionHex"]) or # if window in full screen - resize if Connector.SessionIsFullScreen(inSessionHexStr=lRDPConfigurationDictItem["SessionHex"]) or Connector.SessionIsMinimizedScreen(inSessionHexStr=lRDPConfigurationDictItem["SessionHex"]): # If window is minimized - restore # if window in full screen - resize
Connector.SessionIsMinimizedScreen(inSessionHexStr=lRDPConfigurationDictItem["SessionHex"]): # If window is minimized - restore
Connector.SessionScreenSize_X_Y_W_H(inSessionHex=lRDPConfigurationDictItem["SessionHex"], Connector.SessionScreenSize_X_Y_W_H(inSessionHex=lRDPConfigurationDictItem["SessionHex"],
inXInt=10, inYInt=10, inXInt=10, inYInt=10,
inWInt=550, inWInt=550,

Loading…
Cancel
Save