[Script] IsConnectible

19/09/2003 - 18:54 por Ramon Jiménez [MS] | Informe spam
Cortesía de Torgeir Bakken [MVP]

'you should ping first, because the WMI
'connection time-out is *long* if the computer is offline

strComputer = "something"

' ping the computer to see if it is online
If IsConnectible(strComputer, "", "") Then

' error handling the connection to strComputer
On Error Resume Next
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "oot\cimv2")

If Err.Number = 0 Then
On Error Goto 0

' Do your WMI handling here

Else
WScript.Echo sCompName & " is online but not available"
End If
Else
WScript.Echo sCompName & " is not online"
End If


Function IsConnectible(sHost, iPings, iTO)
' Returns True or False based on the output from ping.exe
'
' Author: Alex Angelopoulos/Torgeir Bakken
' 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

If iPings = "" Then iPings = 2
If iTO = "" Then iTO = 750

Const OpenAsASCII = 0
Const FailIfNotExist = 0
Const ForReading = 1

Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
sTemp = oShell.ExpandEnvironmentStrings("%TEMP%")
sTempFile = sTemp & "unresult.tmp"

oShell.Run "%comspec% /c ping.exe -n " & iPings & " -w " & iTO _
& " " & sHost & ">" & sTempFile, 0 , True

Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, _
FailIfNotExist, OpenAsASCII)

sResults = fFile.ReadAll
fFile.Close
oFSO.DeleteFile(sTempFile)

Select Case InStr(sResults,"TTL=")
Case 0 IsConnectible = False
Case Else IsConnectible = True
End Select
End Function


Saludos
Ramon Jimenez
Microsoft Services
 

Leer las respuestas

#1 Alejandro Curquejo [MS MVP]
19/09/2003 - 19:05 | Informe spam
Gracias Ramon ;-)

Alejandro Curquejo
MS MVP DTS
Windows XP


Lo que debería saber acerca del boletín de seguridad de Microsoft MS03-039
http://www.microsoft.com/spain/tech...9-USER.asp

Siempre Aprendiendo ;-)

La informacion contenida en este mensaje se proporciona "tal cual"
sin garantias de ninguna clase y no otorga ningun derecho, usted
asume todo el riesgo de poner en practica lo aqui recomendado

"Ramon Jiménez [MS]" escribió en el mensaje
news:
Cortesía de Torgeir Bakken [MVP]

'you should ping first, because the WMI
'connection time-out is *long* if the computer is offline

strComputer = "something"

' ping the computer to see if it is online
If IsConnectible(strComputer, "", "") Then

' error handling the connection to strComputer
On Error Resume Next
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "oot\cimv2")

If Err.Number = 0 Then
On Error Goto 0

' Do your WMI handling here

Else
WScript.Echo sCompName & " is online but not available"
End If
Else
WScript.Echo sCompName & " is not online"
End If


Function IsConnectible(sHost, iPings, iTO)
' Returns True or False based on the output from ping.exe
'
' Author: Alex Angelopoulos/Torgeir Bakken
' 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

If iPings = "" Then iPings = 2
If iTO = "" Then iTO = 750

Const OpenAsASCII = 0
Const FailIfNotExist = 0
Const ForReading = 1

Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
sTemp = oShell.ExpandEnvironmentStrings("%TEMP%")
sTempFile = sTemp & "unresult.tmp"

oShell.Run "%comspec% /c ping.exe -n " & iPings & " -w " & iTO _
& " " & sHost & ">" & sTempFile, 0 , True

Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, _
FailIfNotExist, OpenAsASCII)

sResults = fFile.ReadAll
fFile.Close
oFSO.DeleteFile(sTempFile)

Select Case InStr(sResults,"TTL=")
Case 0 IsConnectible = False
Case Else IsConnectible = True
End Select
End Function


Saludos
Ramon Jimenez
Microsoft Services


Preguntas similares