Hi All, i am using the following script which was posted on this forum
and it works great, one slight problem i have not been able to get
around is how to make it do the ping silently, as it stands now it
keeps flashing a dos box up every few seconds when it does the pings,
i cant seem to get it to run silent, can anyone advise ?

On Error Resume Next
Dim target, cnt, iCnt
Dim iMsg
Dim iConf
Dim Flds
Dim strHTML
Dim objFSO, objCfgFile, arrIp(256), arrName(256), arrStatus(256)
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objCfgFile = objFSO.OpenTextFile("mon.cfg", ForReading)
WScript.Echo "Started at: " & Now
WScript.Echo "Monitoring Device:" & vbCrLf
'read the config file
iCnt = 0
Do While objCfgFile.AtEndOfStream <> True
strLine = objCfgFile.ReadLine
arrCfgRecord = split(strLine, ",")
' arrCfgRecord = Array(strLine) ' should this work too? investigate
some other time...!
arrIp(iCnt) = Trim(arrCfgRecord(0))
arrName(iCnt) = Trim(arrCfgRecord(1))
arrStatus(iCnt) = TRUE ' For now we assume all systems are
responsive
Wscript.Echo "Device :" & Trim(arrCfgRecord(0)) & vbTab & "Name:" &
vbTab & Trim(arrCfgRecord(1))
if (Len(strLine)>1) then ' Skip empty lines in the config file
iCnt = iCnt + 1
End If
Loop
Set objShell = CreateObject("WScript.Shell")
'The real meat: where we poll the systems every 10 seconds
do
wscript.sleep(1000) 'milliseconds
for cnt = 0 To iCnt -1 'do this as often as you found devices in the
config file
strPing = "ping -l 1 -n 3 -w 500 "
Set objExec = objShell.Run (strPing & arrIp(cnt)) 'we try 3 pings with
a 5 second wait.
strPingResults = LCase(objExec.StdOut.ReadAll)
strTestagainst = "reply from " & arrIp(cnt)
' WScript.Echo arrName(cnt) & " " & arrStatus(cnt) 'debug
If InStr(strPingResults, strTestagainst) Then
' If we can ping the system
if (arrStatus(cnt)=FALSE) then ' if the old
status was that the system was down
WScript.Echo Now & vbTab & " " & "System Is Up: " &
arrName(cnt) ' inform user system is back again
end if
'WScript.Echo arrName(cnt) & " responded to ping." 'debug
'Wscript.Echo "--" & strPingResults
'Wscript.Echo "--" & strTestagainst
arrStatus(cnt)=TRUE ' system
respons, so status is up
Else
if (arrStatus(cnt)=TRUE) then ' if status
was up
WScript.Echo Now & vbTab & " " & "System Is Down: " &
arrName(cnt) ' inform user the system is down.
end if
arrStatus(cnt)=FALSE ' system does not
respond, so status is down
End If
Next
loop
' Clean up variables.
Set iMsg = Nothing
Set iConf = Nothing
Set Flds = Nothing

Re: Hide ping in this script by Tom

Tom
Tue Apr 22 11:53:31 CDT 2008

On Apr 22, 11:32 am, systemtek <dun...@systemtek.co.uk> wrote:
> Hi All, i am using the following script which was posted on this forum
> and it works great, one slight problem i have not been able to get
> around is how to make it do the ping silently, as it stands now it
> keeps flashing a dos box up every few seconds when it does the pings,
> i cant seem to get it to run silent, can anyone advise ?
>
> On Error Resume Next
{code snipped}

There are several errors in the code posted, such that there is no way
it could work as posted. For example, this line ...

Set objExec = objShell.Run (strPing & arrIp(cnt))

throws a runtime error (type mismatch) because the Run method does not
return an object, but it is hidden by the ON Error Resume Next.
Rather, the Run method returns an integer error status result.
Surprisingly, the underlying Ping still runs, but to no avail, since
the intended result is lost.

Also, the next line also would issue a runtime error, if error
trapping weren't defeated ...

strPingResults = LCase(objExec.StdOut.ReadAll)

That is because the target object, objExec, was not bound by the
previous statement.

The way I would address the problem is to make use of one more of the
command line utilities, along with Ping, as such ...

do
wscript.sleep 1000 'milliseconds NOTE: 1000 ms = 1 second
for cnt = 0 To iCnt -1
If IsConnectible(arrIp(iCnt),3,500) Then
' If we can ping the system
if (arrStatus(cnt)=FALSE) then
WScript.Echo Now & vbTab & " " _
& "System Is Up: " & arrName(cnt)
end if
arrStatus(cnt)=TRUE
Else
if (arrStatus(cnt)=TRUE) then
WScript.Echo Now & vbTab & " " _
& "System Is Down: " & arrName(cnt)
end if
arrStatus(cnt)=FALSE
End If
Next
loop

Function IsConnectible(sHost, iPings, iTO)
' Returns True or False based on the output from ping.exe
'
' Author: Alex Angelopoulos/Torgeir Bakken
' Modified significantly by: Tom Lavedas
' Works an "all" WSH versions
' sHost is a hostname or IP
' iPings is number of ping attempts
' iTO is timeout in milliseconds
' if values are set to "", then defaults below used

Dim nRes
If iPings = "" Then iPings = 1
If iTO = "" Then iTO = 250
with CreateObject("WScript.Shell")
nRes = .Run("%comspec% /c ping.exe -n " & iPings & " -w " & iTO
_
& " " & sHost & " | find ""TTL="" > nul 2>&1", 0 , True)
end with
IsConnectible = (nRes = 0)

End Function

BTW, this thing pings every second or so, not ever ten seconds.
(Depends on how many machines and how long each ping takes.) The
Sleep should be set to 10000 to do that.

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/

Re: Hide ping in this script by GWold

GWold
Wed Apr 23 12:53:09 CDT 2008

If you're running on XP or greater, you could also use WMI. Here's a ping
function:

Function PingWorkstations(objWorkstationToPing)
Dim objPing, objStatus
Set objPing =
GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2").ExecQuery("SELECT
StatusCode FROM Win32_PingStatus WHERE Address = '" & objWorkstationToPing &
"' AND TimeOut = 20")
For Each objStatus In objPing
If IsNull(objStatus.StatusCode) Or objStatus.StatusCode <> 0 Then
'unreachable
PingWorkstations = 1
Else
'reachable
PingWorkstations = 0
End If
Next 'objStatus In objPing
End Function 'PingWorkstations()

"Tom Lavedas" wrote:

> On Apr 22, 11:32 am, systemtek <dun...@systemtek.co.uk> wrote:
> > Hi All, i am using the following script which was posted on this forum
> > and it works great, one slight problem i have not been able to get
> > around is how to make it do the ping silently, as it stands now it
> > keeps flashing a dos box up every few seconds when it does the pings,
> > i cant seem to get it to run silent, can anyone advise ?
> >
> > On Error Resume Next
> {code snipped}
>
> There are several errors in the code posted, such that there is no way
> it could work as posted. For example, this line ...
>
> Set objExec = objShell.Run (strPing & arrIp(cnt))
>
> throws a runtime error (type mismatch) because the Run method does not
> return an object, but it is hidden by the ON Error Resume Next.
> Rather, the Run method returns an integer error status result.
> Surprisingly, the underlying Ping still runs, but to no avail, since
> the intended result is lost.
>
> Also, the next line also would issue a runtime error, if error
> trapping weren't defeated ...
>
> strPingResults = LCase(objExec.StdOut.ReadAll)
>
> That is because the target object, objExec, was not bound by the
> previous statement.
>
> The way I would address the problem is to make use of one more of the
> command line utilities, along with Ping, as such ...
>
> do
> wscript.sleep 1000 'milliseconds NOTE: 1000 ms = 1 second
> for cnt = 0 To iCnt -1
> If IsConnectible(arrIp(iCnt),3,500) Then
> ' If we can ping the system
> if (arrStatus(cnt)=FALSE) then
> WScript.Echo Now & vbTab & " " _
> & "System Is Up: " & arrName(cnt)
> end if
> arrStatus(cnt)=TRUE
> Else
> if (arrStatus(cnt)=TRUE) then
> WScript.Echo Now & vbTab & " " _
> & "System Is Down: " & arrName(cnt)
> end if
> arrStatus(cnt)=FALSE
> End If
> Next
> loop
>
> Function IsConnectible(sHost, iPings, iTO)
> ' Returns True or False based on the output from ping.exe
> '
> ' Author: Alex Angelopoulos/Torgeir Bakken
> ' Modified significantly by: Tom Lavedas
> ' Works an "all" WSH versions
> ' sHost is a hostname or IP
> ' iPings is number of ping attempts
> ' iTO is timeout in milliseconds
> ' if values are set to "", then defaults below used
>
> Dim nRes
> If iPings = "" Then iPings = 1
> If iTO = "" Then iTO = 250
> with CreateObject("WScript.Shell")
> nRes = .Run("%comspec% /c ping.exe -n " & iPings & " -w " & iTO
> _
> & " " & sHost & " | find ""TTL="" > nul 2>&1", 0 , True)
> end with
> IsConnectible = (nRes = 0)
>
> End Function
>
> BTW, this thing pings every second or so, not ever ten seconds.
> (Depends on how many machines and how long each ping takes.) The
> Sleep should be set to 10000 to do that.
>
> Tom Lavedas
> ===========
> http://members.cox.net/tglbatch/wsh/
>

Re: Hide ping in this script by TDM

TDM
Thu Apr 24 14:18:45 CDT 2008


"Tom Lavedas" <tglbatch@cox.net> wrote in message
news:63398d92-e824-4daf-ae8d-2e3488e2fee9@l64g2000hse.googlegroups.com...
> On Apr 22, 11:32 am, systemtek <dun...@systemtek.co.uk> wrote:
>> Hi All, i am using the following script which was posted on this forum
>> and it works great, one slight problem i have not been able to get
>> around is how to make it do the ping silently, as it stands now it
>> keeps flashing a dos box up every few seconds when it does the pings,
>> i cant seem to get it to run silent, can anyone advise ?
>>
>> On Error Resume Next
> {code snipped}
>
> There are several errors in the code posted, such that there is no way
> it could work as posted. For example, this line ...
>
> Set objExec = objShell.Run (strPing & arrIp(cnt))
>
> throws a runtime error (type mismatch) because the Run method does not
> return an object, but it is hidden by the ON Error Resume Next.
> Rather, the Run method returns an integer error status result.
> Surprisingly, the underlying Ping still runs, but to no avail, since
> the intended result is lost.
>
> Also, the next line also would issue a runtime error, if error
> trapping weren't defeated ...
>
> strPingResults = LCase(objExec.StdOut.ReadAll)
>
> That is because the target object, objExec, was not bound by the
> previous statement.
>

And, I am pretty sure the Run method does not support stdOut, I believe
you need to use the Exec method for access to stdOut.


TDM