Как с помощью VB Script проверить доступен ли компьютер в сети или нет. PING

Скрипт будет содержать только одну функцию GetPingStatus(addr), один класс StatusList и массив tStatus(21).

Вот полный листинг VBS скрипта.

Class StatusList
  Dim strNumer
  Dim strDescription
End Class

Dim tStatus(21)

For i = 0 to 21
	Set tStatus(i) = New StatusList
Next

tStatus(0).strNumer = "0": tStatus(0).strDescription = "Success"
tStatus(1).strNumer = "11001": tStatus(1).strDescription = "Buffer Too Small"
tStatus(2).strNumer = "11002": tStatus(2).strDescription = "Destination Net Unreachable"
tStatus(3).strNumer = "11003": tStatus(3).strDescription = "Destination Host Unreachable"
tStatus(4).strNumer = "11004": tStatus(4).strDescription = "Destination Protocol Unreachable"
tStatus(5).strNumer = "11005": tStatus(5).strDescription = "Destination Port Unreachable"
tStatus(6).strNumer = "11006": tStatus(6).strDescription = "No Resources"
tStatus(7).strNumer = "11007": tStatus(7).strDescription = "Bad Option"
tStatus(8).strNumer = "11008": tStatus(8).strDescription = "Hardware Error"
tStatus(9).strNumer = "11009": tStatus(9).strDescription = "Packet Too Big"
tStatus(10).strNumer = "11010": tStatus(10).strDescription = "Request Timed Out"
tStatus(11).strNumer = "11011": tStatus(11).strDescription = "Bad Request"
tStatus(12).strNumer = "11012": tStatus(12).strDescription = "Bad Route"
tStatus(13).strNumer = "11013": tStatus(13).strDescription = "TimeToLive Expired Transit"
tStatus(14).strNumer = "11014": tStatus(14).strDescription = "TimeToLive Expired Reassembly"
tStatus(15).strNumer = "11015": tStatus(15).strDescription = "Parameter Problem"
tStatus(16).strNumer = "11016": tStatus(16).strDescription = "Source Quench"
tStatus(17).strNumer = "11017": tStatus(17).strDescription = "Option Too Big"
tStatus(18).strNumer = "11018": tStatus(18).strDescription = "Bad Destination"
tStatus(19).strNumer = "11032": tStatus(19).strDescription = "Negotiating IPSEC"
tStatus(20).strNumer = "11050": tStatus(20).strDescription = "General Failure"
tStatus(21).strNumer = "default": tStatus(21).strDescription = "Failed"

Function GetPingStatus(addr)
	On Error Resume Next

    Dim wmio , ping,  p 
    Set wmio = GetObject("WinMgmts:{impersonationLevel=impersonate}")
    Set ping = wmio.ExecQuery("SELECT StatusCode FROM Win32_PingStatus WHERE Address = '" & addr & "'")
    For Each p In ping
			For i = 0 To 21
				If cstr(p.StatusCode) = tStatus(i).strNumer Then
				GetPingStatus = tStatus(i).strDescription & " [" & tStatus(i).strNumer & "]"
				End If
			Next
    Next
	If Err.Number <> 0 Then
		GetPingStatus = addr & " : " & Err.Description
	End If

End Function

Dim tmpIP
tmpIP = InputBox("Введите IP адрес" & vbNewLine  & "Пример: 192.168.1.1","Input IP")

If tmpIP <> "" Then
	MsgBox GetPingStatus(tmpIP), vbInformation + vbOKOnly, "GetPingStatus"
End If

Итак разберём скрипт.

В верхней части кода в видим объявление класса

Class StatusList
  Dim strNumer
  Dim strDescription
End Class

Далее мы видим цикл для формирования массива с двумя параметрами

Dim tStatus(21)

For i = 0 to 21
	Set tStatus(i) = New StatusList
Next

Далее мы заполняем массив данными

tStatus(0).strNumer = "0": tStatus(0).strDescription = "Success"
tStatus(1).strNumer = "11001": tStatus(1).strDescription = "Buffer Too Small"
tStatus(2).strNumer = "11002": tStatus(2).strDescription = "Destination Net Unreachable"
tStatus(3).strNumer = "11003": tStatus(3).strDescription = "Destination Host Unreachable"
tStatus(4).strNumer = "11004": tStatus(4).strDescription = "Destination Protocol Unreachable"
tStatus(5).strNumer = "11005": tStatus(5).strDescription = "Destination Port Unreachable"
tStatus(6).strNumer = "11006": tStatus(6).strDescription = "No Resources"
tStatus(7).strNumer = "11007": tStatus(7).strDescription = "Bad Option"
tStatus(8).strNumer = "11008": tStatus(8).strDescription = "Hardware Error"
tStatus(9).strNumer = "11009": tStatus(9).strDescription = "Packet Too Big"
tStatus(10).strNumer = "11010": tStatus(10).strDescription = "Request Timed Out"
tStatus(11).strNumer = "11011": tStatus(11).strDescription = "Bad Request"
tStatus(12).strNumer = "11012": tStatus(12).strDescription = "Bad Route"
tStatus(13).strNumer = "11013": tStatus(13).strDescription = "TimeToLive Expired Transit"
tStatus(14).strNumer = "11014": tStatus(14).strDescription = "TimeToLive Expired Reassembly"
tStatus(15).strNumer = "11015": tStatus(15).strDescription = "Parameter Problem"
tStatus(16).strNumer = "11016": tStatus(16).strDescription = "Source Quench"
tStatus(17).strNumer = "11017": tStatus(17).strDescription = "Option Too Big"
tStatus(18).strNumer = "11018": tStatus(18).strDescription = "Bad Destination"
tStatus(19).strNumer = "11032": tStatus(19).strDescription = "Negotiating IPSEC"
tStatus(20).strNumer = "11050": tStatus(20).strDescription = "General Failure"
tStatus(21).strNumer = "default": tStatus(21).strDescription = "Failed"

Этот массив с кодами ответа при проверки по PING и описанием.

Далее сама функция GetPingStatus(addr)

Function GetPingStatus(addr)
	On Error Resume Next

    Dim wmio , ping,  p 
    Set wmio = GetObject("WinMgmts:{impersonationLevel=impersonate}")
    Set ping = wmio.ExecQuery("SELECT StatusCode FROM Win32_PingStatus WHERE Address = '" & addr & "'")
    For Each p In ping
			For i = 0 To 21
				If cstr(p.StatusCode) = tStatus(i).strNumer Then
					GetPingStatus = tStatus(i).strDescription & " [" & tStatus(i).strNumer & "]"
				End If
			Next
    Next
	If Err.Number <> 0 Then
		GetPingStatus = addr & " : " & Err.Description
	End If

End Function

Вызов функции и получение результатов

Dim tmpIP
tmpIP = InputBox("Введите IP адрес" & vbNewLine  & "Пример: 192.168.1.1","Input IP")

If tmpIP <> "" Then
	MsgBox GetPingStatus(tmpIP), vbInformation + vbOKOnly, "GetPingStatus"
End If

Удачи

Комментарии ()

    386