2015-06-26 4 views
1

мне нужно проверить соединение HTTP с VBScriptпроверка соединения HTTP VBScript

Я думал свистеть к хозяину и увидеть, если хост отвечает

мне нужно проверить подключение к определенному порту и почему не также с адресом

У вас есть решение?

+2

Поиск и вы найдете. [Это было задано вчера.] (Http://stackoverflow.com/questions/31028355/check-network-connection-with-vbscript/31029296) – Bond

ответ

1

Вы можете попробовать так:

Option Explicit 
Dim Title,strHost 
Title = "Check Connection" 
strHost = "www.stackoverflow.com" 
if Ping(strHost) = True then 
    MsgBox "Host " & DblQuote(strHost) & " contacted",vbInformation,Title 
Else 
    MsgBox "Host " & DblQuote(strHost) & " could not be contacted",vbCritical,Title 
end if 
'*********************************************************************************** 
Function Ping(strHost) 
    dim objPing, objRetStatus 
    set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _ 
     ("select * from Win32_PingStatus where address = '" & strHost & "'") 
    for each objRetStatus in objPing 
     if IsNull(objRetStatus.StatusCode) or objRetStatus.StatusCode <> 0 then 
     Ping = False 
      'WScript.Echo "Status code is " & objRetStatus.StatusCode 
     else 
      Ping = True 
      'Wscript.Echo "Bytes = " & vbTab & objRetStatus.BufferSize 
      'Wscript.Echo "Time (ms) = " & vbTab & objRetStatus.ResponseTime 
      'Wscript.Echo "TTL (s) = " & vbTab & objRetStatus.ResponseTimeToLive 
     end if 
    next 
End Function 
'*********************************************************************************** 
Function DblQuote(Str) 
    DblQuote = Chr(34) & Str & Chr(34) 
End Function 
'************************************************************************************ 
+0

Мне нужно проверить http-соединение с определенным портом на моем url – Galene

+0

Посмотрите на этом ==> http://stackoverflow.com/questions/12010631/command-line-for-looking-at-specific-port – Hackoo

+0

И это в powershell ==> http://stackoverflow.com/questions/9566052/ как к сети проверить-порта-доступ-и-дисплей-полезное-сообщение – Hackoo

1

Я вдохновлен этим ==>How to check Network port access and display useful message?

И я создал VBScript, обернутый сценарий Powershell

Просто дать попробовать:

Option Explicit 
Dim Title,Ws,ByPassPSFile,strHost,Example,PSFile,MyCmd,Result,MyArray,LogFile,fso 
Title = "Check Network port access " 
Set Ws = CreateObject("wscript.Shell") 
Set fso = Createobject("Scripting.FileSystemObject") 
LogFile = Left(Wscript.ScriptFullName, InstrRev(Wscript.ScriptFullName, ".")) & "txt" 
PSFile = Left(Wscript.ScriptFullName, InstrRev(Wscript.ScriptFullName, ".")) & "ps1" 
ByPassPSFile = "cmd /c PowerShell.exe -ExecutionPolicy bypass -noprofile -file " 
Example = "www.google.com:80" 
strHost = InputBox("Enter the host name with its port to check it " & vbcr & "Example : " & vbcr & Dblquote(Example) & "",Title,Example) 
If strHost = "" or IsEmpty(strHost) Then Wscript.Quit() 
MyArray = Split(strHost,":") 
MyCmd = "function Test-Port($hostname,$port)"& VbCrLF &_ 
"{"& VbCrLF &_ 
    "# This works no matter in which form we get $host - hostname or ip address" & VbCrLF &_ 
    "try {"& VbCrLF &_ 
     "$ip = [System.Net.Dns]::GetHostAddresses($hostname) |"& VbCrLF &_ 
     "select-object IPAddressToString -expandproperty IPAddressToString"& VbCrLF &_ 
     "if($ip.GetType().Name -eq ""Object[]"")"& VbCrLF &_ 
     "{"& VbCrLF &_ 
      "#If we have several ip's for that address, let's take first one"& VbCrLF &_ 
      "$ip = $ip[0]"& VbCrLF &_ 
     "}"& VbCrLF &_ 
    "} catch {"& VbCrLF &_ 
     "return ""Possibly $hostname is wrong hostname or IP"""& VbCrLF &_ 
    "}"& VbCrLF &_ 
    "$t = New-Object Net.Sockets.TcpClient"& VbCrLF &_ 
    "# We use Try\Catch to remove exception info from console if we can't connect"& VbCrLF &_ 
    "try"& VbCrLF &_ 
    "{"& VbCrLF &_ 
     "$t.Connect($ip,$port)"& VbCrLF &_ 
    "} catch {}"& VbCrLF &_ 
    "if($t.Connected)"& VbCrLF &_ 
    "{"& VbCrLF &_ 
     "$t.Close()"& VbCrLF &_ 
     "$msg = ""Port $port is operational on $hostname with ip adress $ip"""& VbCrLF &_ 
    "}"& VbCrLF &_ 
    "else"& VbCrLF &_ 
    "{"& VbCrLF &_ 
     "$msg = ""Port $port on $hostname with ip $ip is closed, """& VbCrLF &_ 
     "$msg += ""You may need to contact your IT team to open it."""& VbCrLF &_   
    "}"& VbCrLF &_ 
    "return $msg"& VbCrLF &_ 
"}"& VbCrLF &_ 
"Test-Port "& MyArray(0) & " "& MyArray(1) & " > "& LogFile &""& VbCrLF 
Call WriteMyPSFile(MyCmd) 
Result = Ws.run(ByPassPSFile & PSFile,0,True) 
ws.run LogFile 
'********************************************************************************************** 
Sub WriteMyPSFile(strText) 
Dim fs,ts,PSFile 
Const ForWriting = 2 
    PSFile = Left(Wscript.ScriptFullName, InstrRev(Wscript.ScriptFullName, ".")) & "ps1" 
    Set fs = CreateObject("Scripting.FileSystemObject") 
    Set ts = fs.OpenTextFile(PSFile,ForWriting,True) 
    ts.WriteLine strText 
    ts.Close 
End Sub 
'********************************************************************************************** 
Function DblQuote(Str) 
    DblQuote = Chr(34) & Str & Chr(34) 
End Function 
'********************************************************************************************** 
Смежные вопросы