开发者

Ping script with loop and save in a 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.

For Example:

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

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

Now i try a little bit:

   strComputer = "TestPC"

    Set objPing = GetObject("winmgm开发者_JAVA技巧ts:{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

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

I hope some one can help me.

Greeting, matthias


Try this

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


You put your pings inside a loop of some kind and then use Wscript.Sleep 2000 to sleep for 2 seconds.

Then you use the File System Object (FSO) to write to a file. Information can be found here.

Edit: Something like this might work:

Const OpenFileForAppending = 8 
Dim fso, ts
Set fso = CreateObject("Scripting. FileSystemObject")

While 1 > 0 ' loop forever      
    Set ts = fso.OpenTextFile("c:\temp\test.txt", OpenFileForAppending, True)

    ' do your pinging code

    'if ok
        ts.WriteLine("OK")
    'else
        ts.WriteLine("Not OK")
    'endif

    ts.Close()
    Wscript.Sleep 2000
Wend
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜