代码之家  ›  专栏  ›  技术社区  ›  wagnert

VBScript-检查端口是否正在侦听+窗口以通知用户

  •  1
  • wagnert  · 技术社区  · 8 年前

    我有一个关于vbs的问题。 我需要监视端口5900上的连接是否已建立。 脚本应作为服务运行,并应经常检查连接是否已建立。 当建立连接时,它应该打开一个小窗口(不允许用户交互(例如,无法关闭,没有“确定”按钮等)),其中包含类似“建立连接//[连接的设备的主机名]”的内容。 如果连接已关闭,则小窗口也应关闭。

    如果我运行脚本并且建立了连接,ATM就可以弹出消息

    迄今为止我的代码:

    Dim ObjExec
    Dim strFromProc
    
    Set objShell = CreateObject("WScript.Shell")
    Set ObjExec = objShell.Exec("%comspec% /c netstat -a | find ""ESTABLISHED"" | find "":5900""" )
    
    Do Until ObjExec.Stdout.atEndOfStream
        strFromProc = strFromProc & ObjExec.StdOut.ReadLine & vbNewLine
        WScript.Echo "Connection Established"
    Loop
    

    顺致敬意,

    1 回复  |  直到 8 年前
        1
  •  1
  •   Hackoo    8 年前

    尝试这样的方法:使用 .Popup method

    Option Explicit
    Dim ObjExec,objShell,strFromProc,intButton,Port
    Port = "5900"
    Set objShell = CreateObject("WScript.Shell")
    Set ObjExec = objShell.Exec("%comspec% /c netstat -a | find "& DblQuote("ESTABLISHED") & "| find " & DblQuote(Port) &"")
    strFromProc = ObjExec.StdOut.ReadAll
    If Instr(strFromProc,"ESTABLISHED") > 0 Then
        intButton = objShell.Popup(strFromProc,3,"Connection Established @ Port "& Port &"",vbInformation)
    Else    
        intButton = objShell.Popup("Connection Not Established @ Port "& Port,3,"Connection Not Established @ Port "& Port &"",vbExclamation)
    End If  
    '****************************************************************
    Function DblQuote(Str)
        DblQuote = Chr(34) & Str & Chr(34)
    End Function
    '****************************************************************
    

    如果你想避免使用黑色控制台;试试这个方法:

    Option Explicit
    Dim ObjExec,strCommand,OutPutData,objShell,strFromProc,intButton,Port
    Port = "5900"
    Set objShell = CreateObject("WScript.Shell")
    strCommand = "%comspec% /c netstat -a | find "& DblQuote("ESTABLISHED") & "| find " & DblQuote(Port) &""
    OutPutData = Run_Cmd(strCommand)
    If Instr(OutPutData,"ESTABLISHED") > 0 Then
        intButton = objShell.Popup(OutPutData,3,"Connection Established @ Port "& Port &"",vbInformation)
    Else    
        intButton = objShell.Popup("Connection Not Established @ Port "& Port,3,"Connection Not Established @ Port "& Port &"",vbExclamation)
    End If  
    '****************************************************************
    Function DblQuote(Str)
        DblQuote = Chr(34) & Str & Chr(34)
    End Function
    '****************************************************************
    Function Run_Cmd(strCommand)
        On Error Resume Next
        Const ForReading = 1
        Const TemporaryFolder = 2
        Const WshHide = 0
        Dim wsh, fs, ts
        Dim strTempFile,strFile, strData
        Set wsh = CreateObject("Wscript.Shell")
        Set fs = CreateObject("Scripting.FileSystemObject")
        strTempFile = fs.BuildPath(fs.GetSpecialFolder(TemporaryFolder).Path, fs.GetTempName)
        strFile = fs.BuildPath(fs.GetSpecialFolder(TemporaryFolder).Path, "result.txt")
        wsh.Run "cmd.exe /c " & strCommand & " > " & DblQuote(strTempFile) & "2>&1", WshHide, True
        wsh.Run "cmd.exe /u /c Type " & DblQuote(strTempFile) & " > " & DblQuote(strFile) & "", WshHide, True
        Set ts = fs.OpenTextFile(strFile, ForReading,true,-1)
        strData = ts.ReadAll
        Run_Cmd = strData
        ts.Close
        fs.DeleteFile strTempFile
        fs.DeleteFile strFile
    End Function
    '****************************************************************