Sign in to follow this  
Followers 0
smcan

Loop portqry.exe until condition met or 30 times

9 posts in this topic

Looking for some help in getting the microsoft utility portqry.exe to loop until it returns "LISTENING".
 
I currently have a batch file that launches a connection to an SSH server using putty and creates a tunnel for RDP to a terminal server.  I then loop portqry.exe until it is not %errorlevel% == 0.  When that condition is met, it will run mstsc.exe using a preconfigured .RDP file or after 30 iterations will return an error message that the tunnel was not created.  This works well but I'm looking for a more elegant solution that can be distributed to my users as an exe.  I originally started trying to create this in C++ but was creating it as a console application (don't own Visual C++).  Then I remembered AutoIt and that it can create GUI applications so I started working with AutoIt.  I've got the beginnings of the GUI application and can successfully connect to and close the SSH session from the app but my thick skull is having a really hard time figuring out which type of loop to use for the portqry issue and what return value to check on in the loop since %errorlevel% is for batch and not AutoIt.  I've read through each of the loop types in the documentation and looked at other peoples loop examples but it's just not formalizing in my brain.  I know I could use sleep or something but it's not very elegant.  Portqry will allow it to check for the connection in real time and continue on instead of just waiting some preset amount of time.  Please be patient as I'm brand new to AutoIt and although I've done lot's of "Hello World's" in quite a few different programming and scripting languages, that's about as far as I've gone.  Just to add a little clarification to all that probably unnecessary text,  I know how to make portqry run in autoit but can't figure out how to loop it until a value is met before moving on to the next action.

Share this post


Link to post
Share on other sites



Anyone?

Here's some code I've tried (changed it around quite a bit but none of my changes have worked).  As it is, it runs plink then runs portqry but only appears to iterate once and then both plink and portqry close.  Also, sorry, I think I wrote putty in the orig post but I'm actually using plink.

Func lnchTerm1()
    $qPort = 3391
    $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")
            ExitLoop
        Else
            $intCtr = $intCtr + 1
        EndIf
        MsgBox(0, "SOL", "No tunnel created, sorry!", 10)
        ExitLoop
    WEnd
EndFunc

Share this post


Link to post
Share on other sites

you've got the $retVal right, but notice the ExitLoop is executes no matter what the result is. move the two lines (MsgBox and ExitLoop out of the loop (i.e. after the Wend).

Share this post


Link to post
Share on other sites

Thanks for the reply!  However, I just tried it and I get the error "error: 'ExitLoop' not allowed outside loop."

Share this post


Link to post
Share on other sites

Self explanitory error, but delete the exitloop that is outside the loop :)

Post your script.  It will get a quick response with what to delete.


IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

ok, so i hasted a bit, didn't think it through... and didn't notice you're new around, so welcome to AutoIt and to the forum!

now let's get it right:

If lnchTerm1() Then ; this is how you query the result of the functon
    ; this happens when the function returns "True"
    MsgBox(0, "SOL", "Yey, we are connected!", 10)  ; this is an optional addition
Else
    ; this happens when the function returns "False"
    MsgBox(0, "SOL", "No tunnel created, sorry!", 10)   ; this was moved here
EndIf

Func lnchTerm1()
    $qPort = 3391
    $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")
            ;ExitLoop   ; replace this with next line
            Return True ; add this: if you got to here, then you connected successfully
        Else
            $intCtr = $intCtr + 1
        EndIf
        ;MsgBox(0, "SOL", "No tunnel created, sorry!", 10)  ; move this away, see top of script
        ;ExitLoop   ; remove this completely
    WEnd
    Return False    ; add this: if you got to here, then you could not connect
EndFunc

EDIT: yeah, also what jdelaney said.

Edited by orbs

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Thanks for the welcome and the very helpful reply.  I modified my code to match what you have and it does launch but when the portqry run closes it is also closing the plink run so when the mstsc run's, it can't connect because the tunnel has gone away.  Why would the plink run close?  If I comment out everything after the plink run it's keeps running.

Thanks again!

EDIT: No offense meant but, yeah, I get what jdelaney and you are saying about the self explanatory error but in the post previous to me posting that error, you are the one that told me to move the ExitLoop outside of the loop!

Edited by smcan

Share this post


Link to post
Share on other sites

Thanks for the help and this did solve my initial loop problem so I'll mark it solved and open a new topic about the running of plink not continuing to run after the running of portqry completes.

Share this post


Link to post
Share on other sites

odd indeed. i'll follow your next thread.

Share this post


Link to post
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
Sign in to follow this  
Followers 0