当前位置: 首页 > 知识库问答 >
问题:

检查超文本传输协议连接vbscript

祁乐邦
2023-03-14

我需要检查与vbscript的http连接

我想打给主机看看主机是否有反应

我需要测试到特定端口的连接,为什么不使用url呢

你有解决办法吗?

共有2个答案

宗政欣可
2023-03-14

我的灵感来自此==

我创建了一个用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
'**********************************************************************************************
栾景胜
2023-03-14

你可以这样尝试:

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
'************************************************************************************
 类似资料:
  • 我正在使用GWT和Spring controller来管理http流量。有些请求可能需要很长时间,但我希望在超过给定时间时终止请求。 我如何配置超时Spring。我也使用Apache Tomcat 7.0。我试图在tomcat上inrease最大线程,但有一段时间tomcat工作缓慢,因为请求线程不会死。

  • 我只是有一个关于服务中http请求的结构和处理响应的问题。我正在使用Angular2。alpha46 Typescript(刚刚开始测试-我喜欢它…Ps…。感谢所有一直致力于它并通过github作出贡献的人) 因此,采取以下措施: 登录表单。组成部分ts 从这个组件中,我导入了我的userService,它将容纳我的超文本传输协议请求,以登录用户。 使用者服务ts 我想做的是能够处理http请求之

  • 我的LogCat: 签名密钥(sw)为https://api.dropbox.com/1/shares/dropbox/a.jpg?oauth_consumer_key=2f2y1dyuqhp58ek 我对http没有太多经验。。 因为httpPost=新的httpPost(sw);工作正常,这是否意味着基本字符串签名正确? 还是我错过了什么?

  • 我试图禁用我的AngularJS应用程序中的缓存,但它无法使用以下代码: 当我使用

  • 调用SOAX Web服务,并使用groovy脚本检查Web服务响应的超文本传输协议状态。在《骡子3》中,精彩的剧本是... 骡子4中的等价物是什么?谢谢。

  • httpd是Apache超文本传输协议(HTTP)服务器的主程序。被设计为一个独立运行的后台进程,它会建立一个处理请求的子进程或线程的池。 通常,httpd不应该被直接调用,而应该在类Unix系统中由apachectl调用,在Windows NT/2000/XP/2003中作为服务运行和在Windows 95/98/ME中作为控制台程序运行. 语法 httpd [ -d serverroot ]