tags:

views:

196

answers:

3

Hello,

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("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

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

I hope some one can help me.

Greeting, matthias

+1  A: 

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
ho1
this is a good information, but i'm really bad and new in vbs. Is it possible that you can show me how to do? So i can better lern it.
matthias
@matthias: I'm not very experienced with vbs but I added a quick bit of sample code which might work, at least it should hopefully give you the structure of what's needed.
ho1
thank you, i will try it. can you say me, how i make time and date in it?
matthias
@matthias: `FormatDateTime(Now())` - http://www.w3schools.com/vbScript/func_formatdatetime.asp
ho1
A: 

I try this one, but it doesn't work:

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

While 1 > 0 loop    
    Set ts = fso.OpenTextFile("test.txt", OpenFileForAppending, True)

    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
            ts.WriteLine("Not OK")
        Else
        ts.WriteLine("OK")
        End If

    ts.Close()
    Wscript.Sleep 2000

Can some one help me?

matthias
+1  A: 

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
Tester101
thank you it works fine :-)
matthias