Saturday, January 22, 2011

Mutilple Computers Ping Script

NOTE: Do NOT name the script as 'ping.vbs' as it will use 'ping.vbs' to ping the hosts instead of 'ping' command.


Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
If not objFSO.FileExists("servers.txt") THEN
wscript.echo "Please create a file named 'servers.txt' with one PC name to be pingedper line,"&_ vbcrlf&"with a hard return at the end of each line." wscript.quit end if tempobj="temp.txt"

Set objTextFile = objFSO.OpenTextFile("Server.txt", ForReading)
logfile="results.csv"
Set ofile=objFSO.CreateTextFile(logfile,True)
strText = objTextFile.ReadAll
objTextFile.Close
wscript.echo "Ping batch starting, please be patient. This could take some time to"&_ vbcrlf&"finish, depending on the number of hosts to check. You "_ &"will be "&vbcrlf&"notified upon the completion of this script."
ofile.WriteLine ","&"Ping Report -- Date: " & Now() & vbCrLf
arrComputers = Split(strText, vbCrLF)
for each item in arrcomputers
objShell.Run "cmd /c ping -n 1 -w 1000 " & item & " >temp.txt", 0, True
Set tempfile = objFSO.OpenTextFile(tempobj,ForReading)
Do Until tempfile.AtEndOfStream
temp=tempfile.readall
striploc = InStr(temp,"[")
If striploc=0 Then
strip=""
strip=Mid(temp,striploc,16)
strip=Replace(strip,"[",vbTab)
strip=Replace(strip,"]",vbTab)
strip=Replace(strip,"w",vbTab)
strip=Replace(strip," ",vbTab)
End If

If InStr(temp, "Reply from") Then
ofile.writeline item &strip& vbTab &"Online."
ElseIf InStr(temp, "Request timed out.") Then
ofile.writeline item &strip& vbTab &"No response (Offline)."
ELSEIf InStr(temp, "try again") Then
ofile.writeline item &strip& vbTab & "Unknown host (no DNS entry)."

End If
Loop
Next
tempfile.close objfso.deletefile(tempobj)
ofile.writeline ofile.writeline ","&"Ping batch complete "&now()
wscript.echo "Ping batch completed. The results will now be displayed."

More Info : http://www.msfn.org/board/topic/104877-vbscript-ping-multiple-ip-addresses-and-machine-names/

No comments:

Post a Comment