Ping 一个 IP,弹出消息并将结果保存到 txt 文件 - VBS

Ping an IP, pop-up messages and save results to txt file - VBS

我正在制作一个简单的脚本来 ping 地址并将结果保存在 txt 文件中。

目的只是为了方便不知道如何操作的外行用户:

  1. 打开"run"框;
  2. 打开命令;
  3. ping 地址;
  4. 复制结果;
  5. 粘贴到 txt 文件。

到目前为止,我已经制作了这个 .bat 文件,但我想在 vbs 上使用它以获得更多功能和更好的视觉效果。

.BAT 方法

@ECHO OFF
:LOOPSTART
time /T >> PingTest.txt
ping 1.1.1.1 -n 100 >> %userprofile%\Desktop\PingTest.txt
exit
GOTO LOOPSTART

.VBS 方法

Dim WshShell, i
Set WshShell = CreateObject("WScript.Shell")
    WshShell.Popup "This program will run a connectivity test using PING", 5, "PING TEST!"

Dim strHost, strFile

strHost = "1.1.1.1"
strFile = "ping_test.txt"

PingCall strHost, strFile

Sub PingCall (strHost, outputfile)
    Dim Output, Shell, strCommand, ReturnCode

    Set Output = CreateObject("Scripting.FileSystemObject").OpenTextFile(outputfile, 8, True)
    Set Shell = CreateObject("wscript.shell")
    strCommand = "ping -n 100 " & strHost
    While(True)
            WshShell.Popup "Please, wait while test is being executed...", 1, "Test Running"
        ReturnCode = Shell.Run(strCommand, 0, True)
    Wend
End Sub

我遇到的问题 - 使用 VBS 脚本 -:

  1. 将 ping 结果保存到 .txt 文件;
  2. 显示一条消息,表明测试是 运行。我也想 显示还有多少数据包要发送或有一个盒子 未完成时打开(“请稍等。这将关闭一次 测试已完成...");

就是这样。

我是不是太复杂了?

您可以将此行放入快捷方式

cmd /c ( Ping 127.0.0.1 > testPing.txt ) & ( date /t >> testPing.txt )

您需要使用 WshShell.exec 其中 returns

The WshScriptExec object is returned by the Exec method of the WshShell object. The Exec method returns the WshScriptExec object either once the script or program has finished executing, or before the script or program begins executing.

(来自帮助)

它使您可以访问文本所在的 StdOut。在上面做一个 ReadAll

但是 VBScript 可以自己执行 ping

Set objWMIService = GetObject("winmgmts:\.\root\cimv2")

Set colItems = objWMIService.ExecQuery("Select * From win32_PingStatus where address='104.43.195.251'")
For Each objItem in colItems
    msgbox "Status" & objItem.statuscode & " Time " & objItem.ResponseTime
Next

使用100次。

Do while Counter < 100
    Counter = Counter + 1 
    ...Ping code here ...
Loop

有关可用属性的帮助,在命令提示符中

wmic PATH win32_PingStatus get /?

PingStatus 没有任何方法,但如果有的话

wmic PATH win32_PingStatus call /?

您需要将脚本放入 HTA(基本上是一个名为 script.hta 的网页)并使用计数器更新该网页。你可以从这里窃取代码 https://msdn.microsoft.com/en-us/library/ms976290.aspx

你可以玩这个代码:

Option Explicit
Dim ws,fso,TmpLogFile,Logfile,MyCmd,Webaddress,Param
Set ws = CreateObject("wscript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject") 
TmpLogFile = "TmpFile.txt"
LogFile = Left(Wscript.ScriptFullName,InstrRev(Wscript.ScriptFullName, ".")) & "log"
If fso.FileExists(LogFile) Then fso.DeleteFile LogFile
webaddress = "www.whosebug.com"
Param = "-n 10"
MyCmd = "Title Ping to " & DblQuote(Webaddress) &_
" & Color 9B & Mode con cols=75 lines=3 & Echo Please wait a while the ping to " & DblQuote(Webaddress) & " is in progress ... & Time /T > "& TmpLogFile &_
" & Ping " & Param & " " & Webaddress & " >> "& TmpLogFile &_
" & cmd /U /C Type " & TmpLogFile & " > " & LogFile & " & Del " & TmpLogFile & "" 
ws.Popup "This program will run a connectivity test using PING",5,"PING TEST!",vbInformation
Call Run(MyCmd,1,True)
ws.run LogFile
'**********************************************************************************************
Function Run(StrCmd,Console,bWaitOnReturn)
    Dim ws,MyCmd,Result
    Set ws = CreateObject("wscript.Shell")
'A value of 0 to hide the console
    If Console = 0 Then
        MyCmd = "CMD /C " & StrCmd & ""
        Result = ws.run(MyCmd,Console,bWaitOnReturn)
        If Result = 0 Then
            'MsgBox "Success"
        Else
            'MsgBox "An unknown error has occurred!",16,"An unknown error has occurred!"
        End If
    End If
'A value of 1 to show the console
    If Console = 1 Then
        MyCmd = "CMD /c " & StrCmd & ""
        Result = ws.run(MyCmd,Console,bWaitOnReturn)
        If Result = 0 Then
            'MsgBox "Success"
        Else
            'MsgBox "An unknown error has occurred!",16,"An unknown error has occurred!"
        End If
    End If
    Run = Result
End Function
'**********************************************************************************************
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'**********************************************************************************************