2015-09-12 35 views
1

我想製作一個腳本,它會ping一個IP地址,直到它收到響應。當它發生時,它會啓動另一個名爲「sound.vbs」的腳本。我有兩個問題:Pinging和啓動程序

  1. 我不要 希望cmd窗口在執行ping命令時彈出。
  2. 即使ping失敗,腳本也會關閉,而不是等待一段時間並重試ping。

代碼:

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 

如何解決上市問題?

+1

啓發思考[這個例子](http://stackoverflow.com/a/32302212/2165759)隱藏控制檯窗口並檢索輸出。 – omegastripes

+0

感謝您的幫助,但在等待回覆時,使用Java搜索shell執行,似乎我幾乎完成了,哈哈。 – Justin

回答

3

你可以嘗試這樣的腳本只是修改到你:

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 
'**************************************************** 

Loop a function?