Solved VBS/HTA Timeout

Microsoft Windows xp professional w/serv...
January 17, 2011 at 10:07:41
Specs: windows xp sp3, not an issue
Hi all,
I have a large script, part of which establishes a remote connection to a *nix box, via putty.
My script will check the machine is online first (ping) before attempting to connect.
However with all things being random as they are, sometimes, for whatever reason, be it a machine that has inner troubles or a service that is not started etc, sometime the connection will hang when there is no reply to putty via ssh.
This will cause my entire sub to fail.

My question is therefore, does any one have any sneaky ways to set a time out.
I am running plink via wshshell.exec so i have the terminate option available to me, however as the script run asynchronously with the connect, if plink hangs when establishing a connection, then my script will never proceed to the terminate line.
loops and such all have the same result because of the asynchronous nature of the script/connection.

The only thing i can think of is maybe creating a temp.vbs file with all the whole connection subroutine inside, and then calling that file via exec and passing the connection variables to it, then setting a loop timer and using a status check to determine if to use .terminate. This will be alot of work, and require rewriting large sections, so i am hoping to avoid this + im not even sure it will give the desired results.

Any Ideas?

See More: VBS/HTA Timeout

Report •

January 18, 2011 at 06:53:52
You're mixing up terminology. Synchronous waits for something to terminate before moving on. Asynchronous runs the task and your code side-by-side.

Without seeing your code, I'm guessing you're trying to read from STDOUT/STDERR when there's no data. That'll freeze your script until 4KB hits the buffer, or the pipe closes. There's no way to break yourself out of this, and there's no guard against it.

Your best bet would be to write a function to poll the WshScriptExec.Status to see if the application quit. If it did, you can safely grab the data. If it didn't, you can terminate it and then safely read the data.

How To Ask Questions The Smart Way

Report •

January 18, 2011 at 07:24:47
✔ Best Answer
Q&D HTA example:
<html> <head>
  <title>HTA Test</title>
  <style>h1 { text-align: center; }</style>
  <script language="vbscript">
    Option Explicit
    Dim readProcCount, proc
    readProcCount = 0
    Sub txtOut(txt)
      Dim li : Set li = document.createElement("li")
      li.innerText = txt
    End Sub
    Sub DoLoad()
      window.resizeTo 400, 500
      Set proc = CreateObject("WScript.Shell").Exec("cmd /k dir c:\ /s/b/a")
      document.parentWindow.setTimeout "ReadProc", 10000
    End Sub
    Sub ReadProc()
      If proc Is Nothing Then Exit Sub
      If proc.Status Then 'Process finished
        Do Until proc.StdOut.AtEndOfStream
          txtOut proc.StdOut.ReadLine
      Else 'Process still running
        readProcCount = readProcCount + 1
        If readProcCount >= 6 Then 
          document.parentWindow.setTimeout "ReadProc", 10000
        End If
      End If
    End Sub
    Sub Quit()
      If Not proc Is Nothing Then 
        txtOut "Aborting..."
      End If
    End Sub
</head> <body id="body" onload="DoLoad()">
  <h1>HTA Test</h1>

<br /> <br />
  <ul id="log">
  <input type="button" value="Abort" onclick="Quit()"/> </p>
</body> </html>

How To Ask Questions The Smart Way

Report •

July 22, 2011 at 13:40:11
razor, thanks for this. apologies for the terribly late reply but i have been abroad for a long time.
you never fail to amaze me with your different ways of looking at problems than me and as always many thanks for the example.
I have got very close to a flawless solution using the temp file method mentioned above, but your code looks much more elegant.

ill test it and get back to you after the weekend.

Once again thanks for the wisdom, and i have to say most of the really clever stuff i have learnt from vbs has come from you!

Report •

Related Solutions

July 26, 2011 at 23:36:02
have now had a test and a look over this code. It has definitely given me some good ideas and i hope to work something like this into a re-write of my code.

again...thanks Razor

Report •

Ask Question