Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running an exe in the same window

Status
Not open for further replies.

xwb

Programmer
Jul 11, 2002
6,828
GB
I'm trying to run the following script to find out which addresses are being used on our local network.
Code:
dim objShell
set objShell = CreateObject ("WScript.Shell")

for i=1 to 254
   str = "ping -n1 -w10 192.168.0." & cstr(i)
   WScript.echo str
   objShell.Run str
next
If I cscript it, whenever it encounters a "run", it pops up a new window so I get about 254 windows opening at a rate of knots. What I'd like to happen is for all the results to appear in the cmd window from which it is launched so I can format and pipe the result to a file.

The question is how can I get a .exe to execute in the same window and print its results in that widow
 
For some external programs (such as ping.exe) you can use the strategy of running them via the WScript.Shell's Exec method and then capturing their output. Then you can write the output to the script's console:

pinger.vbs
Code:
'pinger.vbs
'
'Usage: CScript pinger.vbs <ip address>

Option Explicit

Dim objShell, i, strCmd, objExec

Set objShell = CreateObject("WScript.Shell")

For i = 1 to 5
  strCmd = "ping -n 1 -w 10 " & WScript.Arguments(0)
  WScript.Echo strCmd

  Set objExec = objShell.Exec(strCmd)
  Do While objExec.Status = 0
    WScript.Sleep 100
  Loop
  WScript.Echo objExec.StdOut.ReadAll
Next

Set objExec = Nothing
Set objShell = Nothing
This doesn't work with programs that send output to the console device directly, only those that read/write their standard I/O streams.
 
or just check the return code to see if it works.....or finds the machine??

intReturn = WshShell.Run "ping blaa, blaa"
If IntReturn <> 0 Then
''not found??
End If
 
[1] For a scan of that scale, I would propose doing it with persistent output to a file for visula inspection or for further automation to extract useful data.

[2] To run on a single instance of cmdprompt window, it is the natural territory of .bat/.cmd.

[3] There are syntax error in using options -n and -w. The command line should be like this with a space separator after -n and -w, like this.
[tt] ping -n 1 -w 10 192.168.0.1[/tt]

[4] The .bat/.cmd file can be made dynamically.

Hence, the overall construction can be like this.
[tt]
tmpfile=fso.getspecialfolder(2) & "\" & fso.gettempname
tmpfile=tmpfile & ".bat" 'I use .bat for instance

'outfile can be constructed as tmpfile if no need to persist for reference
[green]outfile="d:\test\out.txt" 'suit your need[/green]

set fso=createobject("scripting.filesystemobject")
set ots=fso.opentextfile(tmpfile,2,true)
for i=1 to 254
if i=1 then redir=">" else redir=">>"
str = "ping -n 1 -w 10 192.168.0." & cstr(i) & redir & outfile
ots.writeline str
next
ots.close

createobject("wscript.shell").run "%comspec% /c " & tmpfile,0,true

fso.deletefile tmpfile,true 'clean up temp file
set fso=nothing

'for inspection as an example
'for 254 pings it would already be that big to open with wordpad
createobject("wscript.shell").run "notepad " & outfile
[/tt]
 
i did something similar for an sms adv client check to see if the machines were actually still in use. i ran it all from excel vba and kept the info or ping outputs for each device in the spreadsheet
 
Thanks for the tips. I had something similar to tsuji's suggestion but I thought there might be a simpler method instead of creating a batch file which was executed from vbs.
Actually it is 254*3 because I'm using 0, 1 and 2 as the third number.

Oh well, you win some, you lose some.
 
On winXP or win2003 you may play with WMI:
Const IP = "192.168."
Code:
Dim W, i, j, tstIP, O
Set W = GetObject("winmgmts:{impersonationLevel=impersonate}")
For i = 0 To 2
  For j = 1 To 254
    tstIP = IP & i & "." & j
    For Each O In W.ExecQuery("SELECT * FROM Win32_PingStatus WHERE Address = '"_
     & tstIP & "'")
      WScript.Echo tstIP, O.StatusCode
    Next
  Next 'j
Next 'i

Have a look here:

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Geeze, I had totally missed that he wanted to do some sort of subnet scan...

Wondered why anyone would want to ping some box 256 times!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top