I am trying to run a ping against some servers before attempting to connect
via WMI. When running this script on XP or 2003 it is OK.

'******************
machine = "192.168.0.1"
function ping(arg)
with createobject("wscript.shell")
ping = Not CBool(.run("ping -n 1 " & arg,0,true))
end with
end function

If ping(machine) = true then
WScript.Echo "Valid IP"
else
wscript.echo "Unvalid IP"
end if
'*****************

However when I run it on a W2K machine it only seems to check if the IP
address is a valid address NOT whether the machine actually exists. If you
deliberatly input a unused IP address you still get the message back as
true.

Why is this?

Thanks.

Re: Why does this ping script not work on W2K by Torgeir

Torgeir
Thu Dec 04 12:38:18 CST 2003

Graz wrote:

> (snip)
> function ping(arg)
> with createobject("wscript.shell")
> ping = Not CBool(.run("ping -n 1 " & arg,0,true))
> end with
> end function
>
> If ping(machine) = true then
> WScript.Echo "Valid IP"
> else
> wscript.echo "Unvalid IP"
> end if
> '*****************
>
> However when I run it on a W2K machine it only seems to check if the IP
> address is a valid address NOT whether the machine actually exists. If you
> deliberatly input a unused IP address you still get the message back as
> true.

Hi

Sadly, on Win2k, ping.exe will return errorlevel 0 even if
computer is unavailable (offline/turned off) but defined in DNS (ping will
return "Request timed out.")

From my testing, the only time the function will return a correct "False" value

is where ping returns "Unknown host".

Parsing the output from ping.exe and looking for "TTL=" is the way I have found

that will work in all situations/languages.


The VBScript function IsConnectible in the link below will work on all OS
versions (it uses ping.exe to ping the host and looks for the text "TTL=" to
see if it is online):

http://groups.google.com/groups?selm=3EB2AA54.EBB5ECBC%40hydro.com



--
torgeir
Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of the 1328 page
Scripting Guide: http://www.microsoft.com/technet/scriptcenter



Re: Why does this ping script not work on W2K by Graz

Graz
Thu Dec 04 13:06:36 CST 2003

aha that would explain it :) if I need to do this for 1000s of PCs wont this
be very slow?

thanks for your help

graz

"Torgeir Bakken (MVP)" <Torgeir.Bakken-spam@hydro.com> wrote in message
news:3FCF7F1A.EAA0257@hydro.com...
> Graz wrote:
>
> > (snip)
> > function ping(arg)
> > with createobject("wscript.shell")
> > ping = Not CBool(.run("ping -n 1 " & arg,0,true))
> > end with
> > end function
> >
> > If ping(machine) = true then
> > WScript.Echo "Valid IP"
> > else
> > wscript.echo "Unvalid IP"
> > end if
> > '*****************
> >
> > However when I run it on a W2K machine it only seems to check if the IP
> > address is a valid address NOT whether the machine actually exists. If
you
> > deliberatly input a unused IP address you still get the message back as
> > true.
>
> Hi
>
> Sadly, on Win2k, ping.exe will return errorlevel 0 even if
> computer is unavailable (offline/turned off) but defined in DNS (ping will
> return "Request timed out.")
>
> From my testing, the only time the function will return a correct "False"
value
>
> is where ping returns "Unknown host".
>
> Parsing the output from ping.exe and looking for "TTL=" is the way I have
found
>
> that will work in all situations/languages.
>
>
> The VBScript function IsConnectible in the link below will work on all OS
> versions (it uses ping.exe to ping the host and looks for the text "TTL="
to
> see if it is online):
>
> http://groups.google.com/groups?selm=3EB2AA54.EBB5ECBC%40hydro.com
>
>
>
> --
> torgeir
> Microsoft MVP Scripting and WMI, Porsgrunn Norway
> Administration scripting examples and an ONLINE version of the 1328 page
> Scripting Guide: http://www.microsoft.com/technet/scriptcenter
>
>



Re: Why does this ping script not work on W2K by Torgeir

Torgeir
Thu Dec 04 13:27:46 CST 2003

Graz wrote:

> aha that would explain it :) if I need to do this for 1000s of PCs wont this
> be very slow?

Compared to what? The time difference between your function and mine will be
almost nothing, at least if you set iPings to 1 when calling IsConnectible,
like this:

IsConnectible(strComputer, 1, "")


--
torgeir
Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of the 1328 page
Scripting Guide: http://www.microsoft.com/technet/scriptcenter



Re: Why does this ping script not work on W2K by Roland

Roland
Fri Dec 05 16:35:10 CST 2003

Here is what came up with.

It allows you to test a single IP address or a class C range. I'll probably
end up expanding it to scan Class A and B networks but right now, it is just
too slow this way. I set the ping wait time to 1ms and it still takes over
3 minutes to check a class C range on a private network.

I also included start and ending times so you can see how long it takes to
run.

Option Explicit
'
' Written by Roland Hall
' Copyright (c) 2003 - Dangerously, Inc.
' Free for non-commercial use
'
' Validates a single IP or a range of IP addresses in the 4th Octet
'
' Syntax: pingv ipaddress [2-254]
'
' Ex: pingv 192.168.0.1
' Ex: pingv 192.168.0.1 254
'
Const ForReading = 1
Const wait = 1

Dim PingOut, Ping, ip, oArgs, oShell, fso, file
Dim strFile, strText, strTextArr, strStream, i, s, sLine, sLeft, sTemp
Dim ipArr, ipStart, ipEnd, ipNet
Dim startTime, endTime, x

startTime = now()
PingOut = "c:\ping.txt"
Set oArgs = WScript.Arguments
Set oShell = CreateObject("WScript.Shell")

sub reportHead()
oShell.Run "cmd /c echo Host Alive Ping Results>" & PingOut,0,True
end sub

sub report()
oShell.Run "cmd /c echo | more | ping " & ip & " -n 1 -w " & wait & " |
find ""TTL="">>" & PingOut,0,True
end sub

sub reportIt(x)
select case x
case 0
if InStr(strText,"TTL") Then
endTime = now()
WScript.Echo "Start Time: " & startTime & vbCrLf & _
" End Time: " & endTime & vbCrLf & _
" " & ip & " valid"
else
WScript.Echo "Start Time: " & startTime & vbCrLf & _
" End Time: " & endTime & vbCrLf & _
" " & ip & " not valid"
end if
case 1
endTime = now()
WScript.Echo "Start Time: " & startTime & vbCrLf & _
" End Time: " & endTime & vbCrLf & _
s
case else
end select
end sub

if oArgs.Count = 0 Then
WScript.Echo "You must provide an IP address to ping"
WScript.Quit
elseif oArgs.Count = 2 Then
ipEnd = oArgs(1)
ipArr = Split(oArgs(0),".",-1,1)
ipNet = ipArr(0) & "." & ipArr(1) & "." & ipArr(2) & "."
ipStart = ipArr(3)
reportHead
for i = ipStart to ipEnd
ip = ipNet & i
report
next
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.GetFile(PingOut)
Set strStream = file.OpenAsTextStream(ForReading)
strStream.SkipLine ' Skip Header
Do While Not strStream.AtEndOfStream
sLine = strStream.ReadLine
sLeft = Split(sLine,":",-1,1)
sTemp = Replace(sLeft(0),"Reply from ","") & " is valid"
s = s & " " & sTemp & vbCrLf
Loop
strStream.Close
reportIt 1
else
ip = oArgs(0)
reportHead
report
Set fso = CreateObject("Scripting.FileSystemObject")
Set strFile = fso.OpenTextFile(PingOut, ForReading)
strText = strFile.ReadAll
strFile.Close
reportIt 0
end if

Set oArgs = nothing
Set oShell = nothing
Set fso = nothing
Set strFile = nothing
Set strStream = nothing
Set file = nothing



Re: Why does this ping script not work on W2K by 1

1
Fri Dec 05 17:21:41 CST 2003

On Thu, 4 Dec 2003 18:24:37 -0000, "Graz" <graz@goaway.com> wrote:

>I am trying to run a ping against some servers before attempting to connect
>via WMI. When running this script on XP or 2003 it is OK.
>
>'******************
>machine = "192.168.0.1"
>function ping(arg)
> with createobject("wscript.shell")
> ping = Not CBool(.run("ping -n 1 " & arg,0,true))
> end with
>end function
>
>If ping(machine) = true then
> WScript.Echo "Valid IP"
>else
> wscript.echo "Unvalid IP"
>end if
>'*****************
>
>However when I run it on a W2K machine it only seems to check if the IP
>address is a valid address NOT whether the machine actually exists. If you
>deliberatly input a unused IP address you still get the message back as
>true.
>
>Why is this?
>
>Thanks.
>

I use a freeware utility called alive.exe instead of ping.

http://wettberg.home.texas.net/alive.htm

Re: Why does this ping script not work on W2K by Roland

Roland
Fri Dec 05 19:17:43 CST 2003

New version:

I eliminated shelling out for ping and only shell out if you want to view
the report. I chose to use WMI to do the discovery. I'm not sure if it's
any faster but I now give you the ability to write to screen or just read
the report. Before the report would have to be parsed and then written to
screen. Now, the report is the same in the file as it is on screen.

Option Explicit
'
' Written by Roland Hall
' Copyright (c) 2003 - Dangerously, Inc.
' Free for non-commercial use
'
' Validates a single IP or a range of IP addresses in the 4th Octet
'
' Syntax: pingv ipaddress [2-254 (True|1)|(False|0)]
'
' Ex: pingv 192.168.0.1
' Ex: pingv 192.168.0.1 254
' Ex: pingv 192.168.0.1 10 1
' Ex: pingv 192.168.0.1 25 False
'
' Report will not write to screen unless the 3rd parameter is 1 or True
'
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const wait = 1

Dim objPing, objStatus, PingOut, ip, oArgs, fso, file
Dim oFile, oShell, oExec, strFile, strText, i, s
Dim ipArr, ipStart, ipEnd, ipNet
Dim startTime, endTime, x, reportIt, mResponse

PingOut = "c:\ping.txt"
reportIt = False
Set oArgs = WScript.Arguments

if oArgs.Count = 3 Then
reportIt = CBool(oArgs(2))
end if

if reportIt = True Then
startTime = now()
end if

Set fso = CreateObject("Scripting.FileSystemObject")

sub reportHead()
Set oFile = fso.CreateTextFile(PingOut, True)
oFile.WriteLine("Host Alive Ping Results")
oFile.Close
end sub

function ipStatus(ip)
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
ExecQuery("select * from Win32_PingStatus where address = '"_
& ip & "'")
For Each objStatus in objPing
If IsNull(objStatus.StatusCode) or objStatus.StatusCode<>0 Then
ipStatus = ip & " is not reachable"
else
ipStatus = ip & " is alive!"
End If
Next
end function

sub report(x)
select case x
case 0
WScript.Echo ipStatus(ip)
case 1
Set oFile = fso.OpenTextFile(PingOut, ForAppending, True)
oFile.WriteLine(ipStatus(ip))
oFile.Close
case else
end select
end sub

if oArgs.Count = 0 Then
WScript.Echo "You must provide an IP address to ping"
WScript.Quit
elseif oArgs.Count > 1 Then
ipEnd = oArgs(1)
ipArr = Split(oArgs(0),".",-1,1)
ipNet = ipArr(0) & "." & ipArr(1) & "." & ipArr(2) & "."
ipStart = ipArr(3)
reportHead
Set fso = CreateObject("Scripting.FileSystemObject")
for i = ipStart to ipEnd
ip = ipNet & i
report 1
next
if reportIt = True Then
endTime = now()
Set strFile = fso.OpenTextFile(PingOut, ForReading)
strText = strFile.ReadAll
strFile.Close
WScript.Echo "Start Time: " & startTime & vbCrLf & _
" End Time: " & endTime & vbCrLf & _
strText
else
'WScript.Echo "Process Complete!" & vbCrLf & _
'"Report written to " & PingOut
mResponse = MsgBox("View " & PingOut & "?",vbYesNo + vbQuestion,"Process
Complete")
select case mResponse
case vbYes
Set oShell = CreateObject("WScript.Shell")
Set oExec = oShell.Exec("notepad " & PingOut)
Do While oExec.Status = 0
WScript.Sleep 100
Loop
case else
end select
end if
else
ip = oArgs(0)
reportHead
report 0
end if

Set oArgs = nothing
Set fso = nothing
Set oFile = nothing
Set objPing = nothing
Set strFile = nothing
Set file = nothing
Set oShell = nothing
Set oExec = nothing