我需要检查与vbscript的http连接
我想要ping到主机并查看主机是否响应
我需要测试与特定端口的连接,为什么不测试网址
你有解决方案吗?答案 0 :(得分: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
'************************************************************************************
答案 1 :(得分:1)
我受此启发==&gt; 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
'**********************************************************************************************