Solved

Need help improving small vbs application

Posted on 2004-08-17
4
859 Views
Last Modified: 2010-05-18
I am using the following code to run ping and tracert commands from a browser. Thanks to   DexStar for the code. This works great, but it is pretty slow to respond, since it waits to complete the operation before painting the screen. Can this be modified to paint line-by-line results as they occur in order to speed up the process?

=================
<%@ LANGUAGE="VBSCRIPT" %>
<%
     Dim WshShell : Set WshShell = Server.CreateObject("WScript.Shell")
     
     ' Turn on Page Buffering for better results
     Response.Buffer = True

     Sub ExecCMD( strExec, strTitle )
          Dim proc, n

          ' Write the title, and send it to the browser
          Response.Write "<H1>" & strTitle & "</H1>"
          Response.Flush

          ' Execute the command and wait for it to finish
          Set proc = WshShell.Exec( strExec )
          Do While proc.Status = 0
               ' Anyone know a better way to wait?  WScript.Sleep doesn't work in ASP
               n = n + 1
          Loop

          ' Write the results, and send it to the browser
          Response.Write "<pre>" & proc.StdOut.ReadAll & "</pre>"
          Response.Flush
     End Sub
%>
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
<TITLE>Test Results</TITLE>
</HEAD>
<BODY>
<%
     Dim strIP : strIP = Request.QueryString("IP")
     
   If ( strIP & "" <> "" ) Then
          If ( InStr(strIP, " ") = 0 ) Then
               ExecCMD "cmd /c ping " & strIP, "Ping Results"
               ExecCMD "cmd /c tracert " & strIP, "Trace Route Results"
          Else
               Response.Write "Bad H4x0r, No Ping Flood!"
          End If
     Else
          Response.Write "No IP provided!"
     End If
%>
</BODY>
</HTML>
===============
0
Comment
Question by:VAN
  • 2
  • 2
4 Comments
 
LVL 19

Expert Comment

by:Dexstar
Comment Utility
@Van:

Try the code that I posted below.  It should fix your issue with the delayed output.  I know it will for sure send the results to the browser as they happen, but I'm not sure if the browser will render them until it gets the </pre> tag.  If not, we might have to tweak it a little.  Let me know what you find out.

-D*

===

<%@ LANGUAGE="VBSCRIPT" %>
<%
      Dim WshShell : Set WshShell = Server.CreateObject("WScript.Shell")
      
      ' Turn on Page Buffering for better results
      Response.Buffer = True

      Sub ExecCMD( strExec, strTitle )
            Dim objProc, objOutput

            ' Write the title, and send it to the browser
            Response.Write "<H1>" & strTitle & "</H1>" & vbNewLine
            Response.Flush

            ' Execute the command and wait for it to finish
            Set objProc = WshShell.Exec( strExec )
            Set objOutput = objProc.StdOut
            Response.Write "<pre>"
            Do While objProc.Status = 0
                  ' Read each line of output so far
                  Do While Not objOutput.AtEndOfStream
                        Response.Write objOutput.ReadLine
                  Loop
                  ' Send it to the browser
                  Response.Flush
            Loop

            ' Write rest of the results
            Response.Write objOutput.ReadAll
            Response.Write "</pre>" & vbNewLine
            Response.Flush
      End Sub
%>
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
<TITLE>Test Results</TITLE>
</HEAD>
<BODY>
<%
      Dim strIP : strIP = Request.QueryString("IP")
      
      If ( strIP & "" <> "" ) Then
            If ( InStr(strIP, " ") = 0 ) Then
                  ExecCMD "cmd /c ping " & strIP, "Ping Results"
                  ExecCMD "cmd /c tracert " & strIP, "Trace Route Results"
            Else
                  Response.Write "Bad H4x0r, No Ping Flood!"
            End If
      Else
            Response.Write "No IP provided!"
      End If
%>
</BODY>
</HTML>
0
 
LVL 1

Author Comment

by:VAN
Comment Utility
Thanks for the reply, Dex, but this is giving the ame results. It waits until it has all data before printing it to the screen.
0
 
LVL 19

Accepted Solution

by:
Dexstar earned 500 total points
Comment Utility
This is.  It sends each line with it's own <pre> </pre> tags, so the browser should be able to render each line as it comes in.

===

<%@ LANGUAGE="VBSCRIPT" %>
<%
      Dim WshShell : Set WshShell = Server.CreateObject("WScript.Shell")
      
      ' Turn on Page Buffering for better results
      Response.Buffer = True
      
      Sub ShowOutput( strOutput )
            Response.Write "<pre>" & strOutput & "</pre>" & vbNewLine
            Response.Flush
      End Sub

      Sub ExecCMD( strExec, strTitle )
            Dim objProc, objOutput

            ' Write the title, and send it to the browser
            Response.Write "<h1>" & strTitle & "</h1>" & vbNewLine
            Response.Flush

            ' Execute the command and wait for it to finish
            Set objProc = WshShell.Exec( strExec )
            Set objOutput = objProc.StdOut
            Do While objProc.Status = 0
                  ' Write each line of output so far
                  Do While Not objOutput.AtEndOfStream
                        ShowOutput objOutput.ReadLine
                  Loop
            Loop

            ' Write rest of the results
            ShowOutput objOutput.ReadAll
      End Sub
%>
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
<TITLE>Test Results</TITLE>
</HEAD>
<BODY>
<%
      Dim strIP : strIP = Request.QueryString("IP")
      
      If ( strIP & "" <> "" ) Then
            If ( InStr(strIP, " ") = 0 ) Then
                  ExecCMD "cmd /c ping " & strIP, "Ping Results"
                  ExecCMD "cmd /c tracert " & strIP, "Trace Route Results"
            Else
                  Response.Write "Bad H4x0r, No Ping Flood!"
            End If
      Else
            Response.Write "No IP provided!"
      End If
%>
</BODY>
</HTML>
0
 
LVL 1

Author Comment

by:VAN
Comment Utility
That's it! Thanks, Dex!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now