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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remote machines... 1

Status
Not open for further replies.

Staticfactory

IS-IT--Management
Mar 1, 2005
79
CA
What is the most efficient way of checking to see if a remote machine is alive or not? The way my script works right now, it just attempts to connect to the remote computer and processes an OnError routine should it not work. Unfortunately, this causes the script to run slowly as it takes a while for the script to come back with an error, especially when I run a job on 100 machines.

I would like to replace this routine with a simple ping or comm check that will take a fraction of the time. What would you guys recommend?
 
Use a ping test via a shell:

Here's a quick and dirty function I wrote to do this, returns TRUE if the machine responds, FALSE if it doesn't...

function fPingTest( strComputer )
dim objShell,objPing
dim strPingOut, flag
set objShell = CreateObject("Wscript.Shell") set objPing = objShell.Exec("ping "& strComputer)
strPingOut = objPing.StdOut.ReadAll
if instr(LCase(strPingOut), "reply") then
flag = TRUE
else
flag = FALSE
end if
fPingTest = flag
end function

-e
 
Cool, I'll see how much stress this function can take off my script. Thanks EnochL.
 
add this to the function I gave you earlier, change the paths of the input/output variables, and feed it a list of computer names in a text file, 1 to a line...it should output the data you need:


Code:
dim strInputPath, strOutputPath, strStatus
dim objFSO, objTextIn, objTextOut

strInputPath = "c:\serverlist.txt") '- location of input
strOutputPath = "c:\output.csv") '- location of output

set objFSO = CreateObject("Scripting.FileSystemObject")
set objTextIn = objFSO.OpenTextFile( strInputPath,1 )
set objTextIn = objFSO.CreateTextFile( strOutputPath )
objTextOut.WriteLine("computer,status")

Do until objTextIn.AtEndOfStream = True
	strComputer = objTextIn.ReadLine
        if fPingTest( strComputer ) then
             strStatus = "UP"
        else
             strStatus = "DOWN"
        end if
        objTextOut.WriteLine(strComputer & "," & strStatus)
loop
 
The first function you gave me will likely be perfect. I'm just using the function to test the machines before I try to connect to them to, say, list the local administrator accounts. When I was just trying to connect and using an error routine, it was taking forever... now, if the machine isn't replying, the script won't even bother trying to connect.

Thanks again.
 
Just tested it... the cmd window is a bit of a pain in the butt. Is there another way to ping a machine without using an external program?
 
For WinXP and 2003:
For Each O In GetObject("winmgmts:{impersonationLevel=impersonate}")._
ExecQuery("select * from Win32_PingStatus where Address = '"_
& strComputer & "'")
If O.StatusCode = 0 Then strStatus = "UP" Else strStatus = "DOWN"
Next

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I was going to try that but it's only valid on XP and 2003 networks.
 
As far as I've been able to find, there's no way to do a silent ping on a 2000/XP/2003 network. Exec'ing the ping from the command line works but I run scripts against hundreds of computers and the cmd window popping up is quite bothersome. If anyone has a suggestion regarding a silent and quick ping/isAlive idea I'm all ears.
 
This works for me when I check that a remote machine is live before collect disk usage stats

I cant take credit for this as I posed a similar question some time ago but it works just fine.

Si


' Ping
' Function to Secretely call up the commnad shell and ping a server
' This is need for compatibility sake. Currently only XP and 2003 server have built
' in WMI versions

Function ping(hostname)
set wshShell = CreateObject("WScript.Shell")
ping = Not CBool(wshShell.run("ping -n 1 " & hostname,0,True))
End Function

 
I just tested bertieuk's function, and it worked beautifully for me. I think you just need to make sure that the computer you run the script from is xp/2k3, it doesn't matter what the servers are.

Thanks bertieuk! I'm stealing this! LOL

-e
 
Yes, that does seem to work quite nicely indeed! It's too bad that it doesn't support w2k though.. going to have to find a way around that. Thanks for all your help guys!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top