2

I am trying to make a script which pings an IP address until it receives a response. When it does, it launches another script called "sound.vbs". I've got 2 issues:

  1. I don't want the cmd window to pop up when ping command is executed.
  2. Even when ping fails, script simply shuts down instead of waiting some time and retrying the ping.

Code:

Dim objShell
Set objShell = Wscript.CreateObject("WScript.Shell")
Dim target 'define target ip
Dim result 'define ping result

target= "193.105.173.130" 'Archeage EU server IP (possibly Shatigon)
result = "Request timed out" 'Initial result

Set shell = WScript.CreateObject("WScript.Shell") 'create WScript shell
Set shellexec = shell.Exec("ping " & target) 'setting up the ping
Dim count 
count = 1

Do
result = LCase(shellexec.StdOut.ReadAll)

If InStr(result , "reply from") Then
 objShell.Run "sound.vbs" 
 Set objShell = Nothing
 count = count + 1

Else 
WScript.Sleep 4000
End If
Loop until count < 2

How do I solve the listed issues?

hollopost
  • 569
  • 9
  • 28
Justin
  • 533
  • 2
  • 6
  • 16
  • 1
    Consider [this example](http://stackoverflow.com/a/32302212/2165759) to hide console window and retrieve output. – omegastripes Sep 12 '15 at 17:04
  • Thank you for your help, but while waiting for responses, googled about shell execution in Java and seems that I am almost done, lol. – Justin Sep 12 '15 at 18:00

2 Answers2

4

You can try like this script just modify to yours :

Option Explicit
Dim strComputer,objPing,objStatus
strComputer = "smtp.gmail.com"
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}!\\").ExecQuery _
("select * from Win32_PingStatus where address = '" & strComputer & "'")
For Each objStatus in objPing
    If objStatus.Statuscode = 0 Then
        Call MyProgram()
        wscript.quit
    End If
Next
'****************************************************
Sub MyProgram()
    Dim objShell
    Set objShell = CreateObject( "WScript.Shell" )
    objShell.Run("calc.exe")
    Set objShell = Nothing
End Sub
'****************************************************

Inspired from Loop a function?

Community
  • 1
  • 1
Hackoo
  • 18,337
  • 3
  • 40
  • 70
1
If StrComp(right(WScript.FullName,11),"wscript.exe",1) = 0 Then     '' hide the popup of cmd windows
  WScript.Quit CreateObject("WScript.Shell").Run("cscript.exe //nologo """ & WScript.ScriptFullName & """", 0, False)
End If
Dim target 'define target ip
Dim result 'define ping result
target= "8.8.8.8" 'Archeage EU server IP (possibly Shatigon)
result = "Request timed out" 'Initial result
Dim Shell
Set Shell = WScript.CreateObject("WScript.Shell") 'create WScript shell

Dim count 
count = 1

Do 
Set shellexec = Shell.Exec("ping " & target) 'setting up the ping
result = LCase(shellexec.StdOut.ReadAll)
If InStr(1,result , "TTL=",1)> 0 Then
Shell.Run "sound.vbs",0,False
 Exit Do 
Else 
WScript.Sleep 4000
count = count + 1
End If
Loop Until count > 2 

Set Shell = Nothing
WScript.Quit

1-The first 3 line of code hide the popup of command line windows

2-exec the ping command have to done inside do loop not out so you will have second retry if first ping fail

3- Use TTL= instead of replay from (localhost or router can send replay from while target not reachable )

4- until > 2 not less than (infinity loop)

hollopost
  • 569
  • 9
  • 28