带有 WMI 和 VBScript 的 Netstat [英] Netstat with WMI and VBScript

查看:40
本文介绍了带有 WMI 和 VBScript 的 Netstat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on a project where I need to modify a script used for network documentation. The current script that we use is a modified version of SYDI, found here. What I would like to do is add to this script the ability to execute a netstat -an and have it returned with the rest of the report. I was curious if anyone has used WMI and VBScript to return netstat information and how it might be able to be incorporated into this script.

注意:我不是要推销产品,也不隶属于 SYDI 项目.

NOTE: I am not trying to promote a product and I am not affiliated with the SYDI project.

推荐答案

您可以像下面的脚本一样运行 netstat 并捕获结果,但是 activeX 也提供了很多信息,但我需要知道您需要什么信息正是.

You could run netstat and capture the result like the script here under, but much info is also available from activeX but the i would need to know what information you need exactly.

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 的评论这里有一个简化版本

based on the comment of OP here a simplified version

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  

这篇关于带有 WMI 和 VBScript 的 Netstat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆