Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ping output to text file . . . 2

Status
Not open for further replies.

CBOIT

IS-IT--Management
Jul 9, 2010
22
US
Okay, here is the objective:
I want to be able to ping an external list of machines, and if the machines respond, I would like the machine name to be placed in another text file for a different vbs to reference.

Here is what I have so far:
Code:
strMonth = Month(Date)
strDay = Day(Date)
strYear = Right(Year(Date),2)

strFileName = "\\server2006\tech\log\pingresults.txt"


Set Fso = CreateObject("Scripting.FileSystemObject")

Set InputFile = fso.OpenTextFile("systemlist.Txt")



Do While Not (InputFile.atEndOfStream)

HostName = InputFile.ReadLine



Set WshShell = WScript.CreateObject("WScript.Shell")

Ping = WshShell.Run("ping -n 1 " & HostName, 0, True)


Select Case Ping

Case 0 objfileName.writeline %machinename%


End Select


Loop

THe above code has all sorts of errors . . . I am very, very new to this, and any help would be greatly appreciated.

[∞]MP
 
This looks like a VBScript, if so you may get more responses here: forum329
 
Having said that, I don't think the return value of WshShell.Run is going to get what you want. A return value of 0 (zero) indicates that the program you called (ping.exe) terminated normally (whether or not the target computer responded to the ping request). It would only return a non-zero if something went wrong with ping.exe (bad input, abnormal termination, etc).
 
Not a complete solution to your problem, but this shows how to get the ping response:

Code:
Set WshShell = WScript.CreateObject("WScript.Shell")
Set Fso = CreateObject("Scripting.FileSystemObject")

'VBScript regular expression reference: [URL unfurl="true"]http://msdn.microsoft.com/en-us/library/ms974570.aspx[/URL]
Set re = new regexp
re.Pattern = "(\d{1,3}%)"
'\d{1,3}% = search for 1, 2, or 3 digits followed by a percent sign
re.IgnoreCase = True

Set objWshScriptExec = WshShell.Exec("ping -n 4 " & "[URL unfurl="true"]www.google.com")[/URL]

'create object to catch external program's output
Set objStdOut = objWshScriptExec.StdOut

Do While Not objStdOut.AtEndOfStream
	strLine = objStdOut.ReadLine
	if re.Test(strLine) then
	
	Set Matches = re.Execute(strLine)
		for each Match in Matches
			if Match.Value = "100%" then
				wscript.echo("no response")
			else
				wscript.echo("computer responded")
			end if
		next
	
	end if
	
Loop
 
Thank you jges, I'll try to adapt that a little bit.

[∞]MP
 
You might want to look at my code in thread222-1389680 or in thread222-1560292. Although written in VB rather than VBscript they should be easily ported, and the important bit is the use of WMI's Win32_PingStatus, which is alot more flexible than trying to return the results of a shelled ping command.
 
Here is a little script based on strongm's excellent code (this is almost a pure cut and paste). Thanks strongm!

Code:
Option Explicit

if IsReachable("[URL unfurl="true"]www.google.com")[/URL] then
	Call vbPing("[URL unfurl="true"]www.google.com")[/URL]
else
	wscript.echo("no response")
end if


'**************************************************************************************************
Function IsReachable(strComputer) ' strComputer can be name or IP address
    Dim objWMIService
    Dim objPing
    Dim objStatus
    
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
    Set objPing = objWMIService.ExecQuery("Select * From Win32_PingStatus Where Address = '" & strComputer & "' and StatusCode=0")
    
    For Each objStatus In objPing
        IsReachable = objStatus.StatusCode = 0
    Next
End Function
'**************************************************************************************************
Sub vbPing(sTarget)

    Dim cPingResults   'collection of instances of Win32_PingStatus class
    Dim oPingResult   'single instance of Win32_PingStatus class
    Dim strMsg
    
    
    Set cPingResults = GetObject("winmgmts:{impersonationLevel=impersonate}//./root/cimv2").ExecQuery("SELECT * FROM Win32_PingStatus WHERE Address = '" + sTarget + "'")

    For Each oPingResult In cPingResults
        strMsg = ""
        If oPingResult.StatusCode = 0 Then
            If LCase(sTarget) = oPingResult.ProtocolAddress Then
                strMsg = strMsg & sTarget & " is responding" & vbCrLf
            Else
                strMsg = strMsg & sTarget & " (" & oPingResult.ProtocolAddress & ") is responding" & vbCrLf
            End If
            strMsg = strMsg & "Bytes = " & oPingResult.BufferSize & vbCrLf
            strMsg = strMsg & "Time (ms) = " & oPingResult.ResponseTime & vbCrLf
            strMsg = strMsg & "TTL (s) = " & oPingResult.ResponseTimeToLive
        Else
            strMsg = strMsg & sTarget & " is not responding"
        End If
    Next
    MsgBox strMsg, vbOKOnly, "Pinging " & sTarget

End Sub
 
Here's the code I've been using for that. I may have gotten it from one of George's threads:

Code:
    Dim OutPut As String
    ' This procedure ("doConnect" in the RMS Version) ORIGINALLY just sent out a
    ' "Winsock1.Connect" command to determine if any connection could be made to the
    ' IP Address that was passed in as a parameter.  NOW, we send a ping command to a
    ' CMD console and look for a certain string output to be returned indicating
    ' success or failure
       
    With CreateObject("wscript.shell")
        AllocConsole
        ShowWindow GetConsoleWindow, 0
        DoEvents
        With .Exec("ping " & strLocalIPaddress & " -n 1 -w 100")
            OutPut = .StdOut.ReadAll
        End With
    End With
    ' "(0% loss" indicates that the "ping" was successful
    If InStr(1, OutPut, "(0% loss") > 0 Then
        CartPinged = True
        LogEvent "Pinged IPAddress: '" & strLocalIPaddress & "' SUCCEEDED"
    Else
        CartPinged = False
        LogEvent "Pinged IPAddress: '" & strLocalIPaddress & "' FAILED"
    End If

HTH

Chew


10% of your life is what happens to you. 90% of your life is how you deal with it.
 
Okay . . . I guess that I wasn't clear on the result I needed. I need the script to not echo anything at all if the machine doesn't respond - but if it does, I need it to echo the IP of the machine, or it's name. I'm going to have another VBScript look at this list. I'm sorry for the trouble! Thank you so much for all of your ideas!

[∞]MP
 
The 2nd section of code I posted was by way of example, you don't have to echo the output; do what you want with it!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top