Wednesday, September 16, 2009

Revised script for IP range ping

Last year i actually wrote a script to do the same thing but the script is not really good and always have the annoyed dos windows pop-up. This time, i using the example on the "Hey, Scripting Guy!" and it is a much much better script in terms of neatness and speed) for doing the same thing


'Create a file to write the result
'============================================================

FileName = "PingResult.txt"
Set fso = CreateObject("Scripting.FileSystemObject")

If (fso.FileExists(FileName)) Then
Set resultFile = fso.OpenTextFile(FileName, 8)
else
Set resultFile = fso.CreateTextFile(FileName, true)
end if


subnetID = "192.168.35."

For range = 2 to 254 ' specify range of ip
ipAddress = subnetID & range
if Ping(ipAddress) then
resultFile.writeline ipAddress & " is alive"
else
resultFile.writeline ipAddress & " is dead"
end if
next

'Using Win32_PingStatus class
'============================================================
Function Ping(ipAddress)

Set objWMIService = _
GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery _
("Select * from Win32_PingStatus " & _
"Where Address = '" & ipAddress & "'")
For Each objItem in colItems
If objItem.StatusCode = 0 Then
Ping = True
else
Ping = False
End If
Next

End Function

No comments: