shell - Ping 和启动程序

标签 shell command-line vbscript

我正在尝试编写一个脚本来 ping IP 地址,直到收到响应。当它执行时,它会启动另一个名为“sound.vbs”的脚本。我有 2 个问题:

  1. 我不 希望执行 ping 命令时弹出 cmd 窗口。
  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

如何解决列出的问题?

最佳答案

你可以尝试像这个脚本一样修改你的:

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?

关于shell - Ping 和启动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32540459/

相关文章:

bash - Atom Snippets 不适用于 Shell 脚本

linux - 如何与乘法增量并行运行

linux - 如何在 Linux 中从命令行打开一个新窗口(shell)?

html - 如何将 HTML 文件作为文本文件打开以使用 VBScript 读取代码?

python - 嵌套的 for 循环,设置变量和 if else 在批处理脚本中

android - 从 Android 终端禁用设备所有者应用程序

linux - 将文本添加到特定文本行

windows - 使用批处理文件设置壁纸目录

python - 命令行中的Python无法切换

c# - 如何以编程方式从 .NET 注册表中删除 Windows 产品 key ?