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

时间:2016-05-09 00:45:24

标签: vbscript ping connectivity

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

目的仅仅是为了让那些不知道如何的非专业用户方便:

  1. 打开“运行”框;
  2. open cmd;
  3. ping一个地址;
  4. 复制结果;
  5. 过去到txt文件。
  6. 到目前为止,我已经创建了这个.bat文件,但是我想在vbs上获得更多功能和更好的视觉效果。

    .BAT APPROACH

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

    .VBS APPROACH

    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. 显示一条消息,指示测试正在运行。我想要 显示仍有多少数据包要发送或有一个盒子 当它没有完成时打开(“请等一下。这将关闭一次 测试结束了......“);
    3. 就是这样。

      我是不是太复杂了?

2 个答案:

答案 0 :(得分:2)

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

WshShell.exec

您需要使用返回

ReadAll
  

WshScriptExec对象由WshShell对象的Exec方法返回。 Exec方法在脚本或程序执行完毕后或脚本或程序开始执行之前返回WshScriptExec对象。

(来自帮助)

它允许您访问文本所在的StdOut。在它上面做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

然而,VBScript可以自己执行ping操作

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

使用做100次。

wmic PATH win32_PingStatus get /?

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

wmic PATH win32_PingStatus call /?

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

MONTH,EVENT,DURATION
2015-03,Event1,1030
2015-03,Event2,365
2015-03,Event9,1010

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

答案 1 :(得分:1)

您可以使用此代码:

TTL
相关问题