Ping和启动计划

时间:2015-09-12 15:22:49

标签: shell command-line vbscript

我正在尝试创建一个ping IP地址的脚本,直到收到响应为止。当它发生时,它会启动另一个名为“sound.vbs”的脚本。我有两个问题:

  1. 我没有 想要在执行ping命令时弹出cmd窗口。
  2. 即使ping失败,脚本也只是关闭而不是等待一段时间并重试ping。
  3. 代码:

    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
    

    如何解决列出的问题?

2 个答案:

答案 0 :(得分:4)

你可以试试这个脚本只需修改你的:

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?

答案 1 :(得分:2)

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-代码的前3行隐藏了命令行窗口的弹出窗口

2- exec ping命令必须在 do 循环内完成,因此如果第一次ping失败,您将进行第二次重试< / p>

3-使用 TTL= 代替 replay from (本地主机或路由器可以在无法访问目标的情况下从发送重播)

4- until > 2 不小于(无限循环)