I have a script that launches plink.exe followed by portqry.exe that checks to make sure the plink created tunnel has been established and then opens mstsc.exe with a preconfigured .RDP file when portqry returns successful (Thanks to orbs for helping get the portqry looping fixed).
However, after the RunWait for portqry completes and exits, the running plink is also being closed and the end result is the mstsc can't connect because the tunnel is no longer there.
Following is the entire script (not entirely complete and sanitized ip's/domain/accounts). Hoping somebody can advise why plink doesn't keep running?
#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
Opt("GUIOnEventMode",1)
$hGUI = GUICreate("Remote Connection",205,286,192,114)
$hBut1 = GUICtrlCreateButton("Launch Term1",16,48,161,49)
$hBut2 = GUICtrlCreateButton("Launch Term2",16,113,161,49)
GUISetState(@SW_SHOW)
GUICtrlSetOnEvent($hBut1, "lnchTerm1Press")
GUISetOnEvent($GUI_EVENT_CLOSE, "ExitGUI")
; This while/wend keeps the gui open
While 1
Sleep(10);
Wend
; Functions
; If Launch term1 button is pressed
func lnchTerm1Press()
If lnchTerm1() Then
MsgBox(0, "YIL", "Yey, we are connected!", 10)
Else
MsgBox(0, "SOL", "No tunnel created, sorry!", 10)
Endif
EndFunc
; Launch Term1 button function
Func lnchTerm1()
$uName = "sshuser"
$pWord = "sshpass"
Run("plink.exe -l " & $uName & " -pw " & $pWord & " -L 3391:192.168.50.25:3389 -P 22 -2 -C -ssh ssh.domain.tld","",@SW_SHOW)
$intCtr = 1
while $intCtr < 30
$retVal = RunWait("portqry.exe -n 127.0.0.1 -e 3391","",@SW_SHOW)
if $retVal = 0 Then
Run("mstsc.exe Term1.RDP")
Return True
Else
$intCtr = $intCtr + 1
EndIf
WEnd
Return False
EndFunc
; Close all plink processes function
Func PlinkClose()
;Close any open plink sessions
While 1
if ProcessExists("plink.exe") Then
ProcessClose("plink.exe")
Else
return False
EndIf
Wend
EndFunc
; Exit program function (closes plink sessions on the way out)
Func ExitGUI()
PlinkClose();
Exit
EndFunc