Ping 一个 IP,弹出消息并将结果保存到 txt 文件 - VBS [英] Ping an IP, pop-up messages and save results to txt file - VBS

查看:142
本文介绍了Ping 一个 IP,弹出消息并将结果保存到 txt 文件 - VBS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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. 打开运行"框;
  2. 打开cmd;
  3. ping 一个地址;
  4. 复制结果;
  5. 传递到 txt 文件.

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

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

.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 脚本 -:

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

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

就是这样.

是不是我把它搞得太复杂了?

Am I complicating it too much?

推荐答案

你可以玩这个代码:

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

这篇关于Ping 一个 IP,弹出消息并将结果保存到 txt 文件 - VBS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆