1

Hy, I want to create batch file to display to the user information about Downloaded packages and Uploaded packages. This info I can get from command 'netstat -e' and I can save this info in log file using 'netstat -e >log.txt'. But I want, when opening this new batch file, that user can see only info about Downloaded and Uploaded packages, not all info from this command, something like this:

Downloaded packages: -info from netstat -e-

Uploaded packages: -info from netstat -e-

Is it possible to cut this info from a log file and put it into batch file when user open this batch? The whole process, after is opening batch file, would look something like this: -save log file with 'netstat -e >log.txt', -getting needed information, -put this info in batch file, -now user see this information.

Methods to do this can be anything, like using cmd and VBS or other.

2 Answers2

1

A VBScript version that scales better, if you need to process and/or display information from netstat (or other console tools) in a more elaborate way:

  ' To simplify the regexp, I have stolen peter's idea of using find; %comspec% needed for |
  Dim sCmd : sCmd = "%comspec% /c netstat -e | find ""packet"""
  ' Shortened .Exec call; works for simple cases
  Dim sAll : sAll = CreateObject("WScript.Shell").Exec(sCmd).Stdout.ReadAll()
  WScript.Echo sAll

  Dim reCut : Set reCut = New RegExp
  reCut.Global  = True
  ' Just get all (4) numbers (non-empty sequences of digits)
  reCut.Pattern = "\d+"
  Dim oMTS : Set oMTS = reCut.Execute(sAll)

  ' Use .NET formatting for nice output
  Dim oSB : Set oSB = CreateObject("System.Text.StringBuilder")
  oSB.AppendFormat_4 _
       "Info from 'netstat -e:{0}{1,21}: {2,12:D}{0}{3,21}: {4,12:D}" _
     , Array( _
           vbCrLf _
         , "Packages received", CLng(oMTS(0).Value) + CLng(oMTS(2).Value) _
         , "Packages sent"    , CLng(oMTS(1).Value) + CLng(oMTS(3).Value) _
       )
  WScript.Echo oSB.ToString()

output:

Unicast packets              488836          264811
Non-unicast packets             650             652

Info from 'netstat -e:
    Packages received:       489486
        Packages sent:       265463
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
0

Just pipe the result of netstat through the find command like this

netstat -e | find "Bytes"

you can still redirect to a textfile like this

netstat -e | find "Bytes" > text.txt

EDIT: based on your comment here more possibilities

No need for a separate program. You can pipe the 2 values with comment to a test or batchfile like this. For /F with the text in parantheses between '' executes the command and lets For parse the result. Tokens=2,3 skips the first result (Bytes) and puts the two others in a separate var

for /F "tokens=2,3" %a in ('netstat -e ^| find "Bytes"') do echo received:%a sent:%b>log.txt

this gives in log.txt

received:74546759 sent:8593498

or you can set the value to environment variables and use it in a batch like

for /F "tokens=2,3" %a in ('netstat -e ^| find "Bytes"') do set received=%a&set sent=%b

set gives then

..
received=75230393
sent=8966725
..

or you can send the values as a parameter to a batch like this

for /F "tokens=2,3" %a in ('netstat -e ^| find "Bytes"') do mybatch %a %b
peter
  • 41,770
  • 5
  • 64
  • 108
  • Okay, with this I can get only Bytes, what I need, to text file, but how I can distinguish Received and Sent packages to get them into new text file (this will be batch file) in their own sections i.e. Received to Downloaded packages and Sent to Uploaded packages.? –  Mar 13 '13 at 11:40
  • Thanks, with first method I get result for I looking. :) –  Mar 13 '13 at 23:01