Jump to content

Remote desktop auto login script I wrote


gte
 Share

Recommended Posts

I posted here, because I'm looking for help as to how to get around a specific problem of reading the contents of the remote desktop window (specifically the legal banner that pops up, and at what exact millisecond it displays and will accept an input key to close) or to somehow tell from the back end, when the OS you are connecting to tells the legal banner registry key to be read and then for the banner to be displayed

I wrote an auto login script for Remote desktop. I also wrote an installer, but I'll only post that if people want it, as it's semi specific to my environment.

I has a couple features, it can encrypt and cache your credentials into an ini file, or you can call the executable with a " /a" after it and it will take what's on your clipboard and automatically push it to the script to log into it. I've tested it on XP, server 2k and server 2k3. I have not had a chance to thoroughly test the console mode switch.

It has switches for console mode, and for keeping the window open after it calls the remote desktop executable and passes the information to it. It can save certain local account credentials and have them via a drop down menu that can be password protected.

The way that I use it, is that I copy a server name to my clipboard and have an icon in my system tray that I click on to launch it, it will then call the remote desktop executable and it waits for me to hit the left hand ctrl key, which then sends the credentials. I had to code it this way, because I could not figure out a way to read the contents inside of the Remote Desktop window and wait until the warning banner appear (as pictured at the bottom of this post) before it sends the necessary key strokes. Some servers take a second to display that, some take as long as 10. And workstations can take even longer sometimes. If someone can figure out a way to read that window inside of the Remote Desktop window, that would be great and I'd be happy to incorporate it into the code.

I'd recommend tweaking the encryption string, and tweaking or at least being aware of where the files/credentials are saved to.

Two images you'll need to save and point the code to (for atheistic purposes) are

Posted Image

and

The .ico attached file that is attached through the forum in this post

remotedesktopicon1.ico

Anyway, let me know what you think and if you have any ideas on how to tell the millisecond (give or take a few) as to when that banner displays the ok button and is ready for a send key input

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_icon=..\remote desktop\remotedesktopicon1L.ico
#AutoIt3Wrapper_outfile=rdc_config_combined.exe
#AutoIt3Wrapper_Res_Comment=Remote desktop auto logon program
#AutoIt3Wrapper_Res_Description=Remote desktop program
#AutoIt3Wrapper_Res_Fileversion=1.0.0.7
#AutoIt3Wrapper_Res_LegalCopyright=2010
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****




#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiComboBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Include <WinAPI.au3>
#Include <string.au3>
#Include <GuiMenu.au3>
#Include <Misc.au3>
#include <Array.au3>

Opt('GUIResizeMode', $GUI_DOCKALL)


Global $default = default
Global $hGUI, $Btn_Options
Global $Checkbox, $Edit
Global $fShow = False
Global $clipboardget = ClipGet( )
Global $clipboard = StringLeft($clipboardget, 15)
Global Const $DM_SETDEFID = 0x0401


_switches()     ; checks for switches and exetcutes that portion first
ConsoleWrite("Now moving beyond the _switches() function, to the while statement ... no switch(es) used" & @CRLF)



#Region ### START Koda GUI section ### Form=c:\documents and settings\668692\my documents\work stuff\autoit\testing\autologinrdc.kxf
$RemoteDesktopScript = GUICreate("Remote Desktop Connection", 413, 500, 617, 309) 
$BannerPic1 = GUICtrlCreatePic(@TempDir & "\rdc1.bmp", -1, -32, 412, 100, BitOR($SS_NOTIFY,$WS_GROUP,$WS_CLIPSIBLINGS))
GUISetIcon(@TempDir & "\remotedesktopicon1.ico")
$ServerNameLabel1 = GUICtrlCreateLabel("Computer:", 8, 80, 52, 21, $WS_TABSTOP)
$ServerNameInput1 = GUICtrlCreateCombo($clipboard, 88, 80, 249, 25) 
$connect = GUICtrlCreateButton("Connect", 88, 112, 75, 25, 0)
GUICtrlSetState($connect, $GUI_FOCUS)
_SendMessage($RemoteDesktopScript, $DM_SETDEFID, $connect)
$cancel = GUICtrlCreateButton("Cancel", 168, 112, 75, 25, 0)
$help = GUICtrlCreateButton("Help", 248, 112, 75, 25, 0)
$options = GUICtrlCreateButton("Options", 328, 112, 75, 25, 0)
$Graphic1 = GUICtrlCreateGraphic(0, 0, 408, 70)
$UsernameInput1 = GUICtrlCreateInput("", 96, 240, 241, 21)
GUICtrlSetState($UsernameInput1, "blank")
$PasswordInput1 = GUICtrlCreateInput("", 96, 272, 241, 21, BitOR($ES_PASSWORD,$ES_AUTOHSCROLL))
GUICtrlSetState($PasswordInput1, "blank")
$DomainInput1 = GUICtrlCreateInput("", 96, 304, 241, 21)
GUICtrlSetState($DomainInput1, "blank")
$User = GUICtrlCreateLabel("User Name", 8, 240, 60, 21)
$Password = GUICtrlCreateLabel("Password", 8, 272, 60, 21)
$Domain = GUICtrlCreateLabel("Domain", 8, 304, 60, 21)
$Checkbox_forsave = GUICtrlCreateCheckbox("", 96, 328, 25, 25)
$Save = GUICtrlCreateLabel("Save these credentials for later use?", 120, 336, 175, 21)
$Checkbox_fornofullscreen = GUICtrlCreateCheckbox("", 8, 360, 17, 17)
$Checkbox_forconsolemode = GUICtrlCreateCheckbox("", 8, 384, 17, 17)
$fullscreen = GUICtrlCreateLabel("Select here if you do NOT want full screen mode", 32, 360, 225, 17)
$consolemode = GUICtrlCreateLabel("Select here if you want to log in with console mode (win2k3 server only)", 32, 384, 343, 17)
$Checkbox_fornoclose = GUICtrlCreateCheckbox("", 8, 408, 17, 17)
$programclose = GUICtrlCreateLabel("Select here if you do NOT want this program to close after connecting", 32, 408, 333, 17)
$localaccounts = GUICtrlCreateCombo("Choose Local Account Credentials", 95, 192, 241, 25)
GUICtrlSetData($localaccounts, "Domain|Other Domain|Another Domain|Etc", "Choose Local Account Credentials") 
GUISetState(@SW_SHOW)
Dim $RemoteDesktopScript_AccelTable[2][2] = [["^n", $connect],["^h", $help]]
GUISetAccelerators($RemoteDesktopScript_AccelTable)
#EndRegion ### END Koda GUI section ###


$decrypted_username = ""
$decrypted_password = ""
$decrypted_domain = ""
$registrylegalpolicy = ""


While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
case $cancel
Exit

case $connect
    If Ping(guictrlread($ServerNameInput1)) < 1 Then        ; checks to see if the server is available via a ping
        MsgBox(0, "Error", "Server not available via ping")
    Else    
    _connect()  
    EndIf
    
case $help
        MsgBox(32, "Help Menu", "This program is an auto sign on program." & @crlf & "This program will load up to the first 15 characters of any text on your clipboard." & @crlf & "When you click on options, you can stored an encrypted version of your pin and password." & @crlf & @crlf & "Click on the options button, for more possibilities")
        GUICreate("Help", "400", "200", -1, -1, $ws_popup, $WS_EX_CONTEXTHELP, $RemoteDesktopScript)
        
Case $options
    $fShow = Not $fShow
            
    If $fshow Then
        GUICtrlSetData($Btn_Options, '&Hide')
        WinMove($RemoteDesktopScript, '', Default, Default, 413, 500)
        
    Else
        GUICtrlSetData($Btn_Options, '&Show')
        WinMove($RemoteDesktopScript, '', Default, Default, 413, 500)
    EndIf

Case $localaccounts
    
    $passwordverification = InputBox("Password Verification", "Please enter the password to access this part of the script", "", "*")
    If $passwordverification = "setpasswordhere" Then   ; set a password here if you want to password protect stored local login values, if you want them wide open, get rid of the If portion
        Select
            Case Guictrlread($localaccounts) = "Domain"     ; set this to a local domain for local admin credentials or however your network works
                GUICtrlSetData($UsernameInput1, "")     ; type the user name you want the script to push to the rdp window, in between quotes
                GUICtrlSetData($PasswordInput1, "")     ; type the password you want the script to push to the rdp window, in between quotes
                GUICtrlSetData($DomainInput1, "")       ; type the domain you want the script to push to the rdp window, in between quotes
            
            Case Guictrlread($localaccounts) = "Other Domain"       ; set this to another domain for local admin credentials
                GUICtrlSetData($UsernameInput1, "")
                GUICtrlSetData($PasswordInput1, "")
                GUICtrlSetData($DomainInput1, "")
        
        EndSelect
        
    Else
        MsgBox(0, "Error", "You can't use this feature unless you were meant to have access")
    
    EndIf

EndSwitch
    
WEnd



func _connect()
    
    if FileExists(@TempDir & "\logon.ini") Then     ; if it doesn't exist, it prompts you to go to the config screen if you run with /a

        $Decrypt_File = FileRead(@TempDir & "\logon.ini")

        $Decrypt_String = _StringEncrypt(0, $Decrypt_File, "putsomethinghere", 7)       ; To encrypt this to whatever you want it to be, replace the "putsomethinghere" with your own strong password like value

        $Decrypt_Array = StringSplit($Decrypt_String, ',')
        _ArrayDisplay($Decrypt_Array)
        $decrypted_username = $Decrypt_Array[1]

        $decrypted_password = $Decrypt_Array[2]

        $decrypted_domain = $Decrypt_Array[3] 

    EndIf

    if GUICtrlread($Checkbox_forsave) = $GUI_CHECKED Then
        FileDelete( @TempDir & "\logon.ini")    ; Deletes the old file first, since it needs to write a new one with new values
        FileWrite( @TempDir & "\logon.ini", _StringEncrypt(1,guictrlread($UsernameInput1) & ',' & guictrlread($PasswordInput1) & ',' & guictrlread($DomainInput1),"putsomethinghere", 7))       ; location the login.ini is stored, in XP it's c:\documents and settings\[profilename]\local settings\temp . Make sure your "putsomethinghere" matches your encryption string
    EndIf

    if GUICtrlRead($Checkbox_fornofullscreen) = $GUI_UNCHECKED then 
        $screenresolution1 = " /f"
    EndIf   
        
    if GUICtrlRead($Checkbox_fornofullscreen) = $GUI_CHECKED then
        $screenresolution1 = " /w:1200 /h:1000"
    EndIf   

    if GUICtrlRead($Checkbox_forconsolemode) = $GUI_UNCHECKED then  
        $consolemode1 = ""
    EndIf   
        
    if GUICtrlRead($Checkbox_forconsolemode) = $GUI_CHECKED then
        $consolemode1 = " /console"
    EndIf   

    $ServerNameInput2=GUICtrlRead($ServerNameInput1)

    $activewindow = WinGetHandle("[active]")

    run( "cmd /c mstsc.exe /v:" & $ServerNameInput2 &$consolemode1 & $screenresolution1, @SystemDir, @SW_HIDE)
    ConsoleWrite("cmd /c mstsc.exe /v:" & $ServerNameInput2 &$consolemode1 & $screenresolution1 & @CRLF)
    If FileExists(@TempDir & "\logon.ini") OR GUICtrlRead($UsernameInput1) <> 0 Then        ; verifies there is no text in the input fields
        $activewindow = WinGetHandle("[active]")
                    
        ConsoleWrite("combo1 - " & $ServerNameInput1 & "combo2 - " & $ServerNameInput2 & @CRLF)
        If FileExists(@TempDir & "\logon.ini") Then     ; only tries to decrypt if you have the file there
            $Decrypt_File = FileRead(@TempDir & "\logon.ini")   ; location the login.ini is stored, in XP it's c:\documents and settings\[profilename]\local settings\temp
            $Decrypt_String = _StringEncrypt(0, $Decrypt_File, "putsomethinghere", 7)   ; To encrypt this to whatever you want it to be, replace the "putsomethinghere" with your own strong password like value
            $Decrypt_Array = StringSplit($Decrypt_String, ',')
            $decrypted_username = $Decrypt_Array[1]
            $decrypted_password = $Decrypt_Array[2]
            $decrypted_domain = $Decrypt_Array[3] 
            ConsoleWrite("logon.ini decrtyped" & @CRLF)
        EndIf
        Consolewrite("Stored values are " & $decrypted_username & " - " & $decrypted_password & " - " & $decrypted_domain & @CRLF)

        ConsoleWrite("Active window is " & $activewindow  & @CRLF)

        WinActivate("[CLASS:SysListView32]")
        ConsoleWrite("Activated desktop itself" & @CRLF)
                    
        ConsoleWrite("Right before control ispressed" & @CRLF) 
        Do
            ConsoleWrite("Waiting for control to be pressed" & @CRLF)
            sleep(50)
            If GUIGetMsg() = $cancel OR GUIGetMsg() = $GUI_EVENT_CLOSE Then     ; this allows the user to hit cancel while the script is running
                Exit
            EndIf                    
        Until _IsPressed("11")      ; this looks for the left hand control key
        ConsoleWrite("Right after control ispressed" & @CRLF) 
        WinActivate($ServerNameInput2 & " - Remote Desktop")
        Sleep(50)
        WinActivate($ServerNameInput2 & " - Remote Desktop")
        Sleep(50)
        ConsoleWrite("WinActivate done" & @CRLF)
        Sleep(50)
        BlockInput(1)
        Send("{ENTER}") 
        Sleep(50)
        Send("!U")
        Sleep(50)
            If GUICtrlRead($UsernameInput1) = "" Then
            Send($decrypted_username,1)
            ConsoleWrite("Sent decrypted username - " & $decrypted_username & @CRLF)
            Else
            Send(GUICtrlRead($UsernameInput1))
            ConsoleWrite("Sent input1 username - " & GUICtrlRead($UsernameInput1) & @CRLF)
            EndIf
                        
        Sleep(50)
        Send("{TAB}")
        Sleep(50)
            If GUICtrlRead($PasswordInput1) = "" Then
            Send($decrypted_password,1)
            ConsoleWrite("Sent decrypted password - " & $decrypted_password & @CRLF)
            Else
            Send(GUICtrlRead($PasswordInput1))
            ConsoleWrite("Sent input2 password - " & GUICtrlRead($PasswordInput1) & @CRLF)
            EndIf                       
            
        Sleep(50)
        Send("{TAB}")
        Sleep(50)
            If GUICtrlRead($DomainInput1) = "" Then
            Send($decrypted_domain,1)
            ConsoleWrite("Sent decrypted domain - " & $decrypted_domain & @CRLF)
            Else
            Send(GUICtrlRead($DomainInput1))
            ConsoleWrite("Sent input3 domain - " & GUICtrlRead($DomainInput1) & @CRLF)
            EndIf            
            
        Sleep(50)
        Send("{TAB}")
        Sleep(50)
        Send("{ENTER}")     
        BlockInput(0)
        Sleep(1000)
        If GUICtrlRead($Checkbox_fornoclose) = $GUI_UNCHECKED then  
            exit
            WinClose("Remote Desktop Connection", "Select here if you do NOT want this program to close after connecting")
            ConsoleWrite("Box was selected to close the RDC window" & @CRLF)
        EndIf   
        If GUICtrlRead($Checkbox_fornoclose) = $GUI_CHECKED then
            ConsoleWrite("Box selected not to close the RDC window after connection was made" & @CRLF)
        EndIf       
        Sleep(2000)
        
    Else
        MsgBox(0,"Error","You need to save your credentials first through the executable, before running from the command prompt with a switch")
    EndIf

EndFunc


    For $i = 1 to 7500 Step 1       ; this polls in case the user wants to use the cancel button while the script is running

        Sleep(1)
        If GUIGetMsg() = $cancel OR GUIGetMsg() = $GUI_EVENT_CLOSE Then
                Exit
        EndIf       
    Next


    if GUICtrlRead($Checkbox_fornoclose) = $GUI_UNCHECKED then  
        exit
        ConsoleWrite("Box was selected to close the RDC window" & @CRLF)
    EndIf   
        
    if GUICtrlRead($Checkbox_fornoclose) = $GUI_CHECKED then
        ConsoleWrite("Box selected not to close the RDC window after connection was made" & @CRLF)
    EndIf   




Func _switches()

    If $CmdLine[0] > 0 Then

            If $CmdLine[1] = "/a" Then
                _connectslasha()                
            ElseIf  $CmdLine[1] = "/c" Then
                MsgBox(0, 0, "Equals /c")               
            elseif $CmdLine[1] = 0 Then
                MsgBox(0, 0, "Equals 0")                
            Else
                MsgBox(0, 0, "???")
            EndIf
    Else
;~      _connectslasha() ; this is here for testing/launching through Scite, since you can't force a command line entry, uncomment the _connectslasha() and comment out the message box below it
        MsgBox(0,"Error", "No command line switch was used, eg: ' /a' ")
    EndIf

EndFunc




func _connectslasha()
    if FileExists(@TempDir & "\logon.ini") Then
    $Decrypt_File = FileRead(@TempDir & "\logon.ini")
    $Decrypt_String = _StringEncrypt(0, $Decrypt_File, "putsomethinghere", 7) ; This decrypts the ini file, make sure the "putsomethinghere" matches what you have as a value to encrypt it with
    $Decrypt_Array = StringSplit($Decrypt_String, ',')
    $decrypted_username = $Decrypt_Array[1]
    $decrypted_password = $Decrypt_Array[2]
    $decrypted_domain = $Decrypt_Array[3] 
    Else
        MsgBox(0,"Error","You need to save your credentials first through the rdc_config executable, before running this shortcut with the clipboard auto paste switch")
        Run("rdc_config.exe", @SystemDir) ; This should be whatever you name your executable as
        Exit
    EndIf

    $screenresolution1 = " /f"


    $ServerNameInput2=ClipGet()
    ConsoleWrite("Server name from clipboard is " & $ServerNameInput2 & @CRLF)

    $activewindow = WinGetHandle("[active]")       
    run( "cmd /c mstsc.exe /v:" & $ServerNameInput2 & $screenresolution1, @SystemDir, @SW_HIDE)
    WinClose("Remote Desktop Connection", "Select here if you do NOT want this program to close after connecting")
    ConsoleWrite("Ran mstsc2 here" & @CRLF)
        If FileExists(@TempDir & "\logon.ini") Then
            $activewindow = WinGetHandle("[active]")
            
                        
            ConsoleWrite("combo2 - " & $ServerNameInput2 & @CRLF)
            $Decrypt_File = FileRead(@TempDir & "\logon.ini")
            $Decrypt_String = _StringEncrypt(0, $Decrypt_File, "ssadmadsusf", 7)
            $Decrypt_Array = StringSplit($Decrypt_String, ',')
            $decrypted_username = $Decrypt_Array[1]
            $decrypted_password = $Decrypt_Array[2]
            $decrypted_domain = $Decrypt_Array[3] 
            ConsoleWrite("logon.ini decrtyped" & @CRLF)
            Consolewrite("Stored values are " & $decrypted_username & " - " & $decrypted_password & " - " & $decrypted_domain & @CRLF)

            ConsoleWrite("Active window is " & $activewindow  & @CRLF)

            WinActivate("[CLASS:SysListView32]")
            ConsoleWrite("Activated desktop itself" & @CRLF)
            ConsoleWrite("Right before control ispressed" & @CRLF) 
            Do
                ConsoleWrite("Waiting for control to be pressed" & @CRLF)
                sleep(50)                
            Until _IsPressed("11") 
            ConsoleWrite("Right after control ispressed" & @CRLF) 
            WinActivate($ServerNameInput2 & " - Remote Desktop")
            Sleep(50)
            WinActivate($ServerNameInput2 & " - Remote Desktop")
            Sleep(50)
            ConsoleWrite("WinActivate done" & @CRLF)
            Sleep(50)
            BlockInput(1)
            Send("{ENTER}") 
            Sleep(50)
            Send("!U")
            Sleep(50)
            Send($decrypted_username,1)
            Sleep(50)
            Send("{TAB}")
            Sleep(50)
            Send($decrypted_password,1)  
            Sleep(50)
            Send("{TAB}")
            Sleep(50)
            Send($decrypted_domain,1)
            Sleep(50)
            Send("{TAB}")
            Sleep(50)
            Send("{ENTER}")     
            BlockInput(0)
            Sleep(1000)
            If GUICtrlRead($Checkbox_fornoclose) = $GUI_UNCHECKED then  
                exit
                WinClose("Remote Desktop Connection", "Select here if you do NOT want this program to close after connecting")
                ConsoleWrite("Box was selected to close the RDC window" & @CRLF)
            EndIf   
            If GUICtrlRead($Checkbox_fornoclose) = $GUI_CHECKED then
                ConsoleWrite("Box selected not to close the RDC window after connection was made" & @CRLF)
            EndIf           
            Sleep(2000)

        EndIf
                
        Exit


EndFunc

This is the banner that displays at various times after the remote desktop window displays

Posted Image

Posted Image

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...