检查http连接vbscript
Posted
技术标签:
【中文标题】检查http连接vbscript【英文标题】:check http connection vbscript 【发布时间】:2015-09-13 10:27:18 【问题描述】:我需要用 vbscript 检查 http 连接
我想 ping 主机并查看主机是否响应
我需要测试到特定端口的连接,为什么不使用 url 来测试
你有解决办法吗?
【问题讨论】:
搜索一下就可以找到。 This was asked yesterday. 【参考方案1】:你可以这样试试:
Option Explicit
Dim Title,strHost
Title = "Check Connection"
strHost = "www.***.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
'************************************************************************************
【讨论】:
我需要检查我的 url 上特定端口的 http 连接 看看这个 ==> ***.com/questions/12010631/… 这在 powershell ==> ***.com/questions/9566052/…【参考方案2】:我受到了启发 ==> How to check Network port access and display useful message?
我创建了一个用 powershell 脚本包装的 vbscript
试一试:
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
'**********************************************************************************************
【讨论】:
以上是关于检查http连接vbscript的主要内容,如果未能解决你的问题,请参考以下文章