Announcement

Collapse
No announcement yet.

scripting tcpvcon

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • scripting tcpvcon

    guys, I just downloaded tcpview, but it comes with a little command "tcpvcon.exe" which I'm trying to run several times a day, kind of taking a tcp snapshot and putting it on a txt file.

    Code:
    echo off
    tcpvcon -anc > C:\tcp_snapshopt.txt
    now, my dilemma is that the file keeps overwriting itself; is there any way to use wildcards or any other method to create like: tcp_snapshot_1.txt, 2.txt, 3.txt, etc.

    Thanks

  • #2
    Re: scripting tcpvcon

    Do you want to create new files or add the output to the same file?
    Marcel
    Technical Consultant
    Netherlands
    http://www.phetios.com
    http://blog.nessus.nl

    MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
    "No matter how secure, there is always the human factor."

    "Enjoy life today, tomorrow may never come."
    "If you're going through hell, keep going. ~Winston Churchill"

    Comment


    • #3
      Re: scripting tcpvcon

      Originally posted by Dumber View Post
      Do you want to create new files or add the output to the same file?
      every time the script runs it suppose to create a new file; therefore, a new different file name.

      Comment


      • #4
        Re: scripting tcpvcon

        You could append a 14 digits long Time Stamp to the file names.

        (file name: tcp_snapshopt_yyyymmddhhnnss.txt )
        Code:
        @echo off & setlocal 
        
        goto:eodt -------------------------------------------------------------
        :dtstamp
        call:dt >%temp%.\dt$.vbs
        >%temp%.\dt$.txt (cscript //nologo %temp%.\dt$.vbs)
        Set /p dtstamp=<%temp%.\dt$.txt
        del %temp%.\dt$.vbs & del %temp%.\dt$.txt
        goto:eof
        :dt
        echo\dt = Now() : dt = Year(dt)*1e4 + Month(dt)*1e2 + Day(dt) _
        echo\   ^& right(1e6+Hour(dt)*1e4+Minute(dt)*1e2+Second(dt),6)
        echo\wsh.echo dt & goto:eof
        :eodt ------------------------------------------------------------------
        call:dtstamp
        
        (tcpvcon.exe -anc )>"C:\tcp_snapshopt_%dtstamp%.txt"
        
        endlocal
        \Rems

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment


        • #5
          Re: scripting tcpvcon

          Originally posted by Rems View Post
          You could append a 14 digits long Time Stamp to the file names.

          (file name: tcp_snapshopt_yyyymmddhhnnss.txt )
          Code:
          @echo off & setlocal 
          
          goto:eodt -------------------------------------------------------------
          :dtstamp
          call:dt >%temp%.\dt$.vbs
          >%temp%.\dt$.txt (cscript //nologo %temp%.\dt$.vbs)
          Set /p dtstamp=<%temp%.\dt$.txt
          del %temp%.\dt$.vbs & del %temp%.\dt$.txt
          goto:eof
          :dt
          echo\dt = Now() : dt = Year(dt)*1e4 + Month(dt)*1e2 + Day(dt) _
          echo\   ^& right(1e6+Hour(dt)*1e4+Minute(dt)*1e2+Second(dt),6)
          echo\wsh.echo dt & goto:eof
          :eodt ------------------------------------------------------------------
          call:dtstamp
          
          (tcpvcon.exe -anc )>"C:\tcp_snapshopt_%dtstamp%.txt"
          
          endlocal
          \Rems
          I tried your script, but doesn't seem to work; it creates a file tcp_snapshot_.txt, no timestamp on it

          Comment


          • #6
            Re: scripting tcpvcon

            Originally posted by miatech View Post
            I tried your script, but doesn't seem to work; it creates a file tcp_snapshot_.txt, no timestamp on it
            The batch sample was tested successfully on Windows XP and Windows Server 2003 and it should be locale undepended.
            This batch is WSH hybide, which however should normally not be of any problem.

            Try:
            replace "setlocal" with:
            Setlocal ENABLEEXTENSIONS

            And add Pause at the end of the batch to be able to see a description of the error.

            * Can you post the errors.
            * What OPERATING SYSTEM versions are you running this batch on??



            ALTERNATIVE SOLUTION
            Below is a different batch. This one does not rely on command processor extensions and WSH support. And it does not make use of temp-files.
            However in this code the variables are pretty fixed and not much flexible.

            Code:
            @echo off & color 72 & setlocal
            
            :: Define Output File
            Set "FileName=tcp_snapshot_###"
            Set "FileExt=txt"
            Set "FilePath=c:"
            
            :: ---------------------------------------------------------------------
            :: *** define DATE STAMP (8 digets) ***
            for /f "tokens=2-4 skip=1 delims=(-./)" %%i in ('echo.^|date') do (
               for /f "tokens=1-4 delims=-./ " %%m in ('date /t') do (
                 (set dow=%%m)&(set %%i=%%n)&(set %%j=%%o)&(set yy=%%p)))
            Set dt=%yy%%mm%%dd%
            :: *** define uniquefying SUCCESSIVE NUMBER (3 digets) ***
            PushD %FilePath% &&(Set SN=%dt%000) ||(goto:eof)
               For /f "tokens=3 delims=_." %%! in (
                 'dir /on /b /a-d "%FileName:_###=_*%.%FileExt%" 2^>nul'
                 ) Do Set "SN=%%!"
               If [%SN:~0,8%] LSS [%dt%] (Set SN=000) Else (Set SN=%SN:~8%)
               Set /a SN= %SN% + 1
               Set "SN=00%SN%"
               Set "SN=%SN:~0,3%"
            PopD
            Set "FileName=%FileName:~0,-4%" & Set "###=%dt%%SN%"
            Set "FileName=%FileName%_%###%"
            IF %ERRORLEVEL% NEQ 0 (goto:eof)
            :: ---------------------------------------------------------------------
            
            Set "outputFile=%FilePath%\%FileName%.%FileExt%"
            
            :startprogram
            (tcpvcon.exe -anc )>"%outputFile%"
            
            endlocal
            \Rems

            This posting is provided "AS IS" with no warranties, and confers no rights.

            __________________

            ** Remember to give credit where credit's due **
            and leave Reputation Points for meaningful posts

            Comment


            • #7
              Re: scripting tcpvcon

              I'm using vista, and I will try what you said. But I have a small bat file, which pretty much I double click, and I get no errors.


              this is what mine contains.

              Code:
              @echo off
              tcpvcon -anc > tcp_shot.txt
              echo ================>> tcp_shot.txt
              nmap -sP 192.168.10.0-255 >> tcp_shot.txt

              Comment


              • #8
                Re: scripting tcpvcon

                I came up with my own and much simplified version...
                Code:
                @ECHO OFF
                :::::::::::::::::::::::::::::::::::::
                :: Taking TCP 3 snapshots s    ::
                ::     by                               ::
                :::::::::::::::::::::::::::::::::::::
                CD tcpshots
                
                :TCP1
                IF EXIST tcpshot1.txt GOTO TCP2
                SET n=1
                tcpvcon -anc > tcpshot%n%.txt
                GOTO NMAP
                
                :TCP2
                IF EXIST tcpshot2.txt GOTO TCP3
                SET n=2
                tcpvcon -anc > tcpshot%n%.txt
                GOTO NMAP
                
                :TCP3
                IF EXIST tcpshot3.txt EXIT
                SET n=3
                tcpvcon -anc > tcpshot%n%.txt
                
                :NMAP
                nmap -sP 10.200.50.0/24 >> tcpshot%n%.txt
                EXIT
                I decided not to put timestamp on the file name, which I found to be a hassle because the limitation of DOS file names; besides if you turn on "details view" on windows explorer you can see the date right there. There's a lot to improve on the code above, I think it can be made even simpler, if you want you can make iterate through a "for loop" instead of creating ":TCP#" tags...

                Comment


                • #9
                  Re: scripting tcpvcon

                  Originally posted by miatech View Post
                  I came up with my own and much simplified version...
                  Code:
                  
                  
                  I decided not to put timestamp on the file name, which I found to be a hassle because the limitation of DOS file names; besides if you turn on "details view" on windows explorer you can see the date right there. There's a lot to improve on the code above, I think it can be made even simpler, if you want you can make iterate through a "for loop" instead of creating ":TCP#" tags...
                  Your solution will add a number just for the purpose to create a unique file name and it fill gaps if there are any. That is OK if this is fine for you.

                  And Yes, instead of adding hundreds of labels you could create some kind of loop. But be aware that there is a 9 digits limit for numeric values for a batch to reliable perform calculations (btw SET /a has more limitations/bugs).
                  Also be ware of the fact that at first your script will only need to check just a few files for an available name for the new file, but the footprint of your running batch will increase if it might have go over a hundred+ files to find one unique name.

                  Here a sample of a loop:
                  Code:
                  @echo off & setlocal
                  
                  :: output file will be: test#.txt
                  set /a n=0
                  :num
                  set /a n+=1
                  IF EXIST "test%n%.txt" (goto:num)
                  
                  Set outputFile=test%n%.txt
                  
                  :: Run Programs, redirect all output to the file
                  >"%outputFile%" (
                   tcpvcon -anc
                   echo\================
                   nmap -sP 192.168.10.0-255
                  )
                  
                  :End Of File
                  However, I think it would be better not to use "IF EXIST" and to use a DIR statement in a FOR-DO loop like it was used in my "alternative" batch, to get the last used number and add it by 1 to use that new number in the name for the new file.

                  FYI,
                  In my first batch the Date&Time stamp was mainly used to create a unique name for the output file, without the need of examining the existing files first. That the file also become better sortable was just a bennefit.

                  And in my "alternative" batch sample I did not use the Timestamp instead it reads the existing file names to determine the successive number to use for the new file. The number was fixed 3 digits long (assuming you wouldn't run the batch more than 999 times on one day) not only for sortabillity reasons but also for the batch to increase possibilities to re-read the value. The Datestamp used in the last sample was just to be able to restart counting on every new day because of the limits of SET /a

                  note the DATE STAMP in the samples does not contain any symbols that are unsupported for file names. Also, the max lenght of the total path+filename for the file system is about 256 characters long, a batch has no problems with long filenames and also support names that have embedded spaces - as long as you keep the "path\filename" between quotes.


                  \Rems



                  EDIT:
                  A good alternative is, using the %RAMDOM% variable for this,
                  http://groups.google.com/group/alt.m...81a632f2bbf6e?

                  _
                  Last edited by Rems; 18th August 2009, 09:56.

                  This posting is provided "AS IS" with no warranties, and confers no rights.

                  __________________

                  ** Remember to give credit where credit's due **
                  and leave Reputation Points for meaningful posts

                  Comment


                  • #10
                    Re: scripting tcpvcon

                    well, I definitely like your second version; it is much simpler, and the "if exist" loop looks efficient in looking for that last file created.... nice!

                    Thanks
                    Last edited by miatech; 3rd August 2009, 21:14.

                    Comment

                    Working...
                    X