<html>
<head><title>Traceroute</title>
<HTA:APPLICATION ID="oHTA";
APPLICATIONNAME="Traceroute";
BORDER="thin";
BORDERSTYLE="normal";
SINGLEINSTANCE="no";
>
</head><body bgcolor="#E8E8E8" >
<font size=2 face="Century Gothic, Tahoma, Arial" color="black">
<script language="VBScript" type="text/vbscript">
set objShell = CreateObject("WScript.Shell")
strOut=""
sub traceroute
cmdarg="%comspec% /c tracert.exe " & T1.value
set objExCmd = objShell.Exec(cmdarg)
strOut=objExCmd.StdOut.ReadAll
Set regEx = New RegExp
regEx.Pattern = "[\f\n\r\v]+"
regEx.Global = True
regEx.Multiline = True
strOut = regEx.Replace(strOut, "<br>")
TraceOut.innerHTML= strOut
end sub
//-->
</script>
<p><b>Traceroute</b><hr noshode color="#000000"><br>
<p>Hostname: <input type="text" size="40" name="T1">
<input type="submit" name="B1" value="Submit" onclick="traceroute"></p>
<div id=TraceOut></div>
<script language="JavaScript">
<!--
if (window.resizeTo) self.resizeTo(600,400);
//-->
</script>
If this were a standalone VBS script, there is a way to do that with a
reentrant procedure, but it's complicated and messy. My advice is
that the easiest way is to convert the application to Run from Exec.
I use the following function to do the job ...
Function CmdPrompt(sCmd)
Dim alines, sCmdLine, stemp, ofs, oWS, nRes
'On Error Resume Next
sCmdLine = """%comspec%"" /c " & sCmd & " 1>> "
set ofs = CreateObject("Scripting.FileSystemObject")
stemp = ofs.GetTempName
set oWS = CreateObject("Wscript.Shell")
stemp = oWS.Environment("PROCESS")("TEMP") & "\" & stemp
nRes = oWS.Run(sCmdLine & Chr(34) & sTemp & Chr(34) _
, Abs(cSng(bConsoleSw)), True)
' optional
' alines = "ERRORLEVEL: " & nRes & vbCRLF
if ofs.FileExists(sTemp) Then
with ofs.OpenTextFile(stemp)
if Not .AtEndofStream Then
alines = aLines & .ReadAll
End if
End With
ofs.DeleteFile stemp
alines = Split(aLines, vbNewline)
Else
aLines = Array(nRes, "")
End if
ReDim Preserve alines(Ubound(alines) - 1)
if Err.Number <> 0 Then _
aLines = Array("Error Number:" & CStr(Err.Number),
Err.Description)
CmdPrompt = alines
End Function
It delivers an array of the output lines of text from the console
application. It returns an array with only one element, containing
the err or errorlevel, if the operation failed.
In your application, add the function's code and it could be applied
thus ...
Replace ...
set objExCmd = objShell.Exec(cmdarg)
strOut=objExCmd.StdOut.ReadAll
with
strOut=Join(CmdPrompt(cmdar), "<br>"))
and the regular expression should be unnecessary (I think) - I'm not
into regular expressions enough to be certain I grok exactly what it
is doing - beside replacing newlines with the <br> HTTP tag.
OK, I reviewed the documentation - it will miss formfeeds, returns
without linefeeds and vertical tabs - but I haven't seen too many of
those issued from MS console apps.
Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/
Nice script, Tom. Just one plea: Could you explain the magic
of "Abs(cSng(bConsoleSw))"?
Thanks
Sorry, I can't - it's a secret ;^))
That bit of code is a holdover from a previous version where I tried
to add a logic switch that would show the console for testing
purposes. What it does is convert the -1 of a logic True to a
nWindowType value of 1 and a logic False would evaluate as a zero.
For example, ...
for each bConsoleSw in array(true, false)
wsh.echo cstr(bConsoleSw), Abs(cSng(bConsoleSw))
next
I never noticed that I failed to remove it when I puled out the other
part that changed the COMSPEC switch from a toggle between /C and /K.
Just replace it with zero.