带有WMI和VBScript的Netstat

时间:2012-05-22 16:46:16

标签: scripting vbscript wmi wmi-query netstat

我正在开发一个项目,我需要修改用于网络文档的脚本。我们使用的当前脚本是SYDI的修改版本found here。我想要做的是在此脚本中添加执行 netstat -an 的功能,并将其与报告的其余部分一起返回。我很好奇是否有人使用WMI和VBScript返回netstat信息以及如何将其合并到此脚本中。

注意:我不是在尝试宣传产品而且我不参与SYDI项目。

1 个答案:

答案 0 :(得分:3)

你可以运行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