用netstat与WMI和VBScript(Netstat with WMI and VBScrip

2019-10-16 17:45发布

我工作的一个项目,我需要修改用于网络文件的脚本。 我们使用当前脚本SYDI修改后的版本, 在这里找到 。 我想要做的就是添加到这个脚本执行的netstat -an的能力,并把它与报告的其余部分退还。 我很好奇,如果有人使用WMI和VBScript返回netstat的信息,以及它如何能够被纳入到这个脚本。

注:我不是一个产品的推广,我不与SYDI项目无关。

Answer 1:

你可以运行netstat这里下捕捉像脚本的结果,但很多信息,也可从的ActiveX,但我需要知道你需要什么样的信息完全吻合。

set sh = CreateObject("Wscript.Shell") 
set Connections = CreateObject("Scripting.Dictionary") 

call Main() 

Function Main() 
    call GetConnections() 
    call ProcessConnections() 
End Function 

Function GetConnections() 
    i = 0 
    set shExec = sh.Exec("netstat -f") 

    Do While Not shExec.StdOut.AtEndOfStream 
        Line = shExec.StdOut.ReadLine() 
        If Instr(Line, "TCP") <> 0 Then 
            Set Connection = New NetworkConnection 
            Connection.ParseText(Line) 
            call Connections.Add(i, Connection) 
            i = i + 1 
        End If 
    Loop 
End Function 

Function ProcessConnections() 
    For Each ConnectionID in Connections.Keys 
        wscript.echo ConnectionID & Connections(ConnectionID).RemoteIP 
    Next 
End Function 

Class NetworkConnection 
    Public Protocol 
    Public LocalIP 
    Public LocalPort 
    Public RemoteIP 
    Public RemotePort 

    Public Sub ParseText(Line) 
        dim i 

        For i = 5 to 2 Step -1 
            Line = Replace(Line, String(i, " "), " ") 
        Next 

        Line = Replace(Line, ":", " ") 
        Line = Right(Line, Len(Line) - 1) 
        Line = Split(Line, " ") 

        Protocol = Line(0) 
        LocalIP = Line(1) 
        LocalPort = Line(2) 
        RemoteIP = Line(3) 
        RemotePort = Line(4) 

    End Sub 

    Private Sub Class_Initialize 
        'MsgBox "Initialized NetworkConnection object" 
    End Sub 

End Class

编辑:基于OP这里的评论简化版

set sh = CreateObject("Wscript.Shell")  
call GetConnections()  

Function GetConnections()  
  i = 0  
  set shExec = sh.Exec("netstat -an")  
   Do While Not shExec.StdOut.AtEndOfStream  
      Wscript.Echo shExec.StdOut.ReadLine()  
  Loop  
End Function  


文章来源: Netstat with WMI and VBScript