Collecting TCP/IP statistics under MS Windows

Blue Bar separator

The netstat-statistics.vbs script will loop forever doing a "netstat -s" every N seconds. Where N is specified as an argument. It is basically a way to monitor the health of the TCP/IP environment and create a baseline of network usage.

To execute the script open a command window and type cscript netstat-statistics.vbs SLEEPTIME, where SLEEPTIME is the time between iterations of netstat. A line giving the date, time and domain\computer name is printed every time the netstat command is run. This is done so you have some idea that something is happening.

C:\noah>cscript netstat-statistics.vbs 5                                  
C:\noah>cscript netstat-statistics.vbs 5
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

============ 2009-5-8 12:28:19 PM - 4400-DEC514859B\4400-DEC514859B============
============ 2009-5-8 12:28:25 PM - 4400-DEC514859B\4400-DEC514859B============
============ 2009-5-8 12:28:30 PM - 4400-DEC514859B\4400-DEC514859B============
============ 2009-5-8 12:28:35 PM - 4400-DEC514859B\4400-DEC514859B============
============ 2009-5-8 12:28:40 PM - 4400-DEC514859B\4400-DEC514859B============
============ 2009-5-8 12:28:45 PM - 4400-DEC514859B\4400-DEC514859B============
============ 2009-5-8 12:28:50 PM - 4400-DEC514859B\4400-DEC514859B============
============ 2009-5-8 12:28:55 PM - 4400-DEC514859B\4400-DEC514859B============
============ 2009-5-8 12:29:01 PM - 4400-DEC514859B\4400-DEC514859B============
^C
C:\noah>
Figure 1 - Example execution with a 5 second sleep time

The netstat output is collected in a file named netstat-statistics.YY-MM-DD.txt, where YY-MM-DD represents the current date in year-month-day format. Each iteration is preceded by a header line giving the date and time and the system's domain\computer-name.

C:\noah>type netstat-statistics.09-5-8.txt
                                 
============ 2009-5-8 12:28:19 PM - 4400-DEC514859B\4400-DEC514859B============

IPv4 Statistics

  Packets Received                   = 96737775
  Received Header Errors             = 0
  Received Address Errors            = 226420
  Datagrams Forwarded                = 0
  Unknown Protocols Received         = 0
  Received Packets Discarded         = 0
  Received Packets Delivered         = 96511365
  Output Requests                    = 2149568
  Routing Discards                   = 0
  Discarded Output Packets           = 0
  Output Packet No Route             = 0
  Reassembly Required                = 0
  Reassembly Successful              = 0
  Reassembly Failures                = 0
  Datagrams Successfully Fragmented  = 0
  Datagrams Failing Fragmentation    = 0
  Fragments Created                  = 0

ICMPv4 Statistics

                            Received    Sent
  Messages                  24259120    114       
  Errors                    0           0         
  Destination Unreachable   23720257    99        
  Time Exceeded             538857      0         
  Parameter Problems        0           0         
  Source Quenches           0           0         
  Redirects                 0           0         
  Echos                     6           0         
  Echo Replies              0           6         
  Timestamps                0           0         
  Timestamp Replies         0           0         
  Address Masks             0           0         
  Address Mask Replies      0           0         

TCP Statistics for IPv4

  Active Opens                        = 23
  Passive Opens                       = 28
  Failed Connection Attempts          = 15
  Reset Connections                   = 21
  Current Connections                 = 5
  Segments Received                   = 1605371
  Segments Sent                       = 1607575
  Segments Retransmitted              = 147

UDP Statistics for IPv4

  Datagrams Received    = 70635415
  No Ports              = 24273291
  Receive Errors        = 8739
  Datagrams Sent        = 541732
============ 2009-5-8 12:28:25 PM - 4400-DEC514859B\4400-DEC514859B============

IPv4 Statistics
. . .
============ 2009-5-8 12:28:30 PM - 4400-DEC514859B\4400-DEC514859B============
. . .
============ 2009-5-8 12:28:35 PM - 4400-DEC514859B\4400-DEC514859B============
. . .
============ 2009-5-8 12:28:40 PM - 4400-DEC514859B\4400-DEC514859B============
. . .
============ 2009-5-8 12:28:45 PM - 4400-DEC514859B\4400-DEC514859B============
. . .
============ 2009-5-8 12:28:50 PM - 4400-DEC514859B\4400-DEC514859B============
. . .
============ 2009-5-8 12:28:55 PM - 4400-DEC514859B\4400-DEC514859B============
. . .
============ 2009-5-8 12:29:01 PM - 4400-DEC514859B\4400-DEC514859B============

C:\noah>
Figure 2 - Example output

Data from two separate invocations of the script done on the same date will be collected into the same file. The script will not delete the file before starting to collect data.

If the script is running when the date changes any new data will be collected into a new file that represents the new current date.

Here is the script. You should be able to copy it, paste it into a file and run it.

REM == netstat-statistics.vbs starts here                                       
REM
REM netstat-statistics.vbs
REM version 1.1 09-05-08
REM version 1.2 10-11-26  added disclaimer
REM Noah Davids - noah.davids@stratus.com
REM
REM This script loops forever doing a "netstat -s" to collect the TCP/IP
REM statistics on the system. It delays for SleepTime seconds at the end of
REM each loop. SleepTime is the only argument. The statistics are collected in
REM a file named netstat-statistics.DATE.txt where DATE is the current date.
REM
REM The current version of this script and documentation may be found at
REM http://noahdavids.org/self_published/netstat-statistics.vbs.html
REM
REM to run the script execute the command
REM           cscript netstat-statistics.vbs SleepTime
REM from a command window.
REM
REM feel free to contact me with any questions or comments
REM
REM This software is provided on an "AS IS" basis, WITHOUT ANY WARRANTY OR ANY SUPPORT OF ANY KIND. 
REM The AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY
REM PARTICULAR PURPOSE.  This disclaimer applies, despite any verbal representations of any kind provided
REM by the author or anyone else.
REM
option explicit
dim SleepTime
dim wshShell
dim WshNetwork
dim Today
dim TodaysDate
dim CurrentTime
dim Message
dim FillerLen
dim Header

Header = "=================================================="

if WScript.Arguments.Count <> 1 then
    WScript.Echo "Usage:"
    WScript.Echo vbtab & "csript netstat-statistics.vbs SleepTime"
    WScript.Echo vbtab & vbtab & "SleepTime is time in seconds between netstats"
    WScript.Echo
else
    SleepTime = WScript.Arguments (0)
    SleepTime = SleepTime * 1000
    Set WshShell = WScript.CreateObject ("WScript.Shell")

    Set WshNetwork = WScript.CreateObject("WScript.Network")
    
    Do While (true)
        Today = Date
        TodaysDate = Year(Today) & "-" & Month (Today) & "-" & Day (Today) 
        CurrentTime = time
        Message = TodaysDate & " " & time & " - " _
            & WshNetwork.UserDomain & "\" & WshNetwork.ComputerName
        FillerLen = (78 - Len (Message)) / 2
	Message = Mid (Header, 1, FillerLen) & " " & Message & Mid (Header, 1, FillerLen)
        WshShell.Run "Cmd.exe /c echo " & Message & " >> netstat-statistics." & _
            TodaysDate & ".txt", 0, True
        WshShell.Run "Cmd.exe /c netstat -s >> netstat-statistics." & TodaysDate & _  
            ".txt", 0, True
        Wscript.Echo Message
        WScript.Sleep SleepTime
    Loop
end if
REM
REM == netstat-statistics.vbs ends here


Blue Bar separator
This page was last modified on 10-11-26
mailbox Send comments and suggestions
to ndav1@cox.net