带循环的 Ping 脚本并保存在 txt 中 [英] Ping script with loop and save in a txt

查看:61
本文介绍了带循环的 Ping 脚本并保存在 txt 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 vbs 制作 Ping 脚本.我需要一个脚本,每 2 秒在网络中 ping(没有 ping 限制,程序将一直运行)一个计算机名并将结果保存在一个 txt 文件中.

i try to make an Ping script with vbs. I need a Script, that ping (no ping limit, the program will run all the time) a computername in the network every 2 seconds and save the results in a txt file.

例如:

06/08/2010 - 13:53:22 |电脑..."在线

06/08/2010 - 13:53:22 | The Computer "..." is online

06/08/2010 - 13:53:24 |计算机..."离线

06/08/2010 - 13:53:24 | The Computer "..." is offline

现在我尝试一下:

   strComputer = "TestPC"

    Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
     ExecQuery("select * from Win32_PingStatus where address = '"_
     & strComputer & "'")
    For Each objStatus in objPing
     If IsNull(objStatus.StatusCode) Or objStatus.StatusCode <> 0 Then 

    ..........          

    Next

而且我不知道如何制作它.(我是 vbs 的新手 :-))

And than i don't know how to make it. (I'm new with vbs :-))

希望有人能帮助我.

您好,马蒂亚斯

推荐答案

试试这个

Option Explicit

Dim strHost, strFile

strHost = "www.google.com" '"127.0.0.1"
strFile = "C:\Test.txt"

PingForever strHost, strFile

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

    Set Output = CreateObject("Scripting.FileSystemObject").OpenTextFile(outputfile, 8, True)
    Set Shell = CreateObject("wscript.shell")
    strCommand = "ping -n 1 -w 300 " & strHost
    While(True)
        ReturnCode = Shell.Run(strCommand, 0, True)     
        If ReturnCode = 0 Then
            Output.WriteLine Date() & " - " & Time & " | The Computer " & strHost & " is online"
        Else
            Output.WriteLine Date() & " - " & Time & " | The Computer " & strHost & " is offline"
        End If
        Wscript.Sleep 2000
    Wend
End Sub

这篇关于带循环的 Ping 脚本并保存在 txt 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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