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

做~自己de王妃 提交于 2021-02-08 09:49:32

问题


I'm making a simple script that pings an address and save the results in a txt file.

The purpose is solely to make it convenient for the lay user that don't know how to:

  1. open the "run" box;
  2. open cmd;
  3. ping an address;
  4. copy results;
  5. past to a txt file.

So far, I have made this .bat file, but I wanted it on vbs for more functionalities and better visual.

.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

Problems I'm having - with the VBS script -:

  1. Save the ping result to the .txt file;
  2. Show a message indicating the test is running. I want to either show how many packets are still to be sent or either to have a box opened while it doesn't finish ("Pls, wait. This will close once test has finished...");

That's it.

Am I complicating it too much?


回答1:


You can play with this code :

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



回答2:


You could put this line into a shortcut

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

You need to use WshShell.exec which 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.

(from help)

It gives you access to StdOut which is where the text is. Do a ReadAll on it.

However VBScript can do it's own pinging

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

Use to do 100 times.

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

For help on what properties are available, in a command prompt

wmic PATH win32_PingStatus get /?

PingStatus doesn't have any methods, but if it did

wmic PATH win32_PingStatus call /?

You would need to put the script into an HTA (basically a web page named script.hta) and update the web page with a counter. You can steal code from here https://msdn.microsoft.com/en-us/library/ms976290.aspx



来源:https://stackoverflow.com/questions/37106383/ping-an-ip-pop-up-messages-and-save-results-to-txt-file-vbs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!