Announcement

Collapse
No announcement yet.

Allowing Input within For Loop

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

  • Allowing Input within For Loop

    Hello,

    I'm trying to log specific output of a program "cdpr.exe", to a file. The issue is, that the cdpr program requires a couple input commands. How can I allow this within the following batch file, and still create my txt file for logging the specific output?

    Code:
    @echo on & SETLOCAL & Title CDPR
    
    SET "sProgram=Cdpr.exe"
    SET "sResults=2xUsers.txt"
    IF NOT exist "%sProgram%" goto :ERROR
    
    FOR /F "delims=" %%A IN ('%sProgram% ^| findstr /b /i /c:"value:"') DO (
      REM ##trim the incipient spaces of %%B and set the value to %%C
      FOR /F "tokens=*" %%C IN ("%%B") DO (
        echo %%C >> %sResults%
      )
    )
    
    :ERROR
       echo There was an error in the path...
       goto:eof

  • #2
    Re: Allowing Input within For Loop

    What kind of input is required? Is it something consistant each time? Can the input be supplied on the command line?

    Comment


    • #3
      Re: Allowing Input within For Loop

      The input may change depending on the machines NIC card. The exe lists all the current NIC cards in a machine, and then you have to choose the number for the NIC card that you want the exe to run against. So for example:

      Please enter number of NIC card to use:
      1. VPN Tunnel Adapter
      2. VMnet Adapter
      3. Local Area Connection1
      4. Local Area Connection2

      So the numbers may be different depending on the machine. And each machine could be using a different NIC than what you would think...so it would have to be made interactive to allow for input of the number, instead of automating a response.

      Comment


      • #4
        Re: Allowing Input within For Loop

        I don't know of any way to achieve that directly with batch file programming. Soemone else may though because I don't know the full ins and outs of all the options.

        What you could do is call cdpr.exe first and have it log the output to a file and tee the output to screen. That would allow you to interact with it.

        After that is complete, run your for loop on the output file and dump to output 2. Delete output 1 and you're there.

        Comment


        • #5
          Re: Allowing Input within For Loop

          That sounds like it could work...I'll see what I can come up with. Thanks.

          Comment


          • #6
            Re: Allowing Input within For Loop

            I tried using "tee.bat" from this site: http://www.robvanderwoude.com/unixports.php#TEE

            And was able to run the input...but no output file was generated and I'm not sure why...

            Maybe I can use the tee.bat and integrate it into the script I was creating...I have this so far and need to figure out changing some of the variables (any help?), still don't have the option for input and not sure what I have to change for it.

            The line "> CON ECHO.%%B" should be redirecting output to the console.

            Code:
            @echo off & SETLOCAL & Title CDPR
            
            SET "sProgram=Cdpr.exe"
            SET "sResults=2xUsers.txt"
            
            :: Test for invalid wildcards
            SET Counter=0
            FOR /F %%A IN ('DIR /A /B %sResults% 2^>NUL') DO CALL :Count "%sResults%"
            IF %Counter% GTR 1 (
            	SET Counter=
            	GOTO Syntax
            )
            
            IF NOT exist "%sProgram%" goto :ERROR
            
            :: tee output to console
            FOR /F "tokens=1* delims=]" %%A IN ('FIND /N /V ""') DO (
            	> CON ECHO.%%B
                    
                    :: find strings and append output to file
                    For /F "delims=" %%B IN ('%sProgram% ^| findstr /b /i /c:"value:"') DO (
                       FOR /F "tokens=*" %%C IN ("%%B") DO (
                          >> %sResults% ECHO.%%C
                       )
                    )
            )
            
            
            :Count
            SET /A Counter += 1
            SET File=%sResults%
            GOTO:EOF
            
            :ERROR
               echo There was an error in the path...
               goto:eof

            Comment


            • #7
              Re: Allowing Input within For Loop

              I think you might have the idea a bit wrong there. Rather than trying to integrate the functionality of tee.bat into your file, I would be more inclined to download the tee.bat and run it side by side.

              Note that I haven't tested it but it should be mostly ok and at the very least show you what I meant.

              Code:
              @echo on & SETLOCAL & Title CDPR
              
              SET "sProgram=Cdpr.exe"
              SET "sTee=tee.bat"
              SET "sTemp=%TEMP%\cdpr-temp.txt"
              SET "sResults=2xUsers.txt"
              IF NOT exist "%sProgram%" goto :ERROR
              IF NOT exist "%sTee%" goto :ERROR
              IF NOT exist "%sTemp%" goto :CONTINUE
              del %sTemp% /q
              :CONTINUE
              
              call %sProgram% |tee %sTemp%
              
              FOR /F "delims=" %%A IN ('findstr /b /i /c:"value:" %sTemp%') DO (
                REM ##trim the incipient spaces of %%B and set the value to %%C
                FOR /F "tokens=*" %%C IN ("%%B") DO (
                  echo %%C >> %sResults%
                )
              )
              
              del %sTemp% /q
              
              :ERROR
                 echo There was an error in the path...
                 goto:eof

              Comment


              • #8
                Re: Allowing Input within For Loop

                Originally posted by beddo View Post
                I think you might have the idea a bit wrong there. Rather than trying to integrate the functionality of tee.bat into your file, I would be more inclined to download the tee.bat and run it side by side.
                Yeah I get that, but I didn't want to have multiple files

                Anyway, the "call" needed to come out, or else the script window just closes immediately after running.

                I think the issue is that the tee.bat isn't creating the tmp file, or writing to it.

                Comment


                • #9
                  Re: Allowing Input within For Loop

                  Try using one of the ports from the package here:

                  http://unxutils.sourceforge.net/

                  They are 32bit though so it possibly depends on your client. I think the one you linked to is somewhat old so may not work with Vista or 7.

                  Comment


                  • #10
                    Re: Allowing Input within For Loop

                    Originally posted by ekrengel View Post
                    Hello,

                    I'm trying to log specific output of a program "cdpr.exe", to a file. The issue is, that the cdpr program requires a couple input commands. How can I allow this within the following batch file, and still create my txt file for logging the specific output?

                    Code:
                    @echo on & SETLOCAL & Title CDPR
                    
                    SET "sProgram=Cdpr.exe"
                    SET "sResults=2xUsers.txt"
                    IF NOT exist "%sProgram%" goto :ERROR
                    
                    FOR /F "delims=" %%A IN ('%sProgram% ^| findstr /b /i /c:"value:"') DO (
                      REM ##trim the incipient spaces of %%B and set the value to %%C
                      FOR /F "tokens=*" %%C IN ("%%B") DO (
                        echo %%C >> %sResults%
                      )
                    )
                    
                    :ERROR
                       echo There was an error in the path...
                       goto:eof
                    The batch below will ask the user to enter a number for the NIC to use, it writes the user's choice to the log file.
                    Then it will pipe the input to the Cdpr.exe command.

                    Code:
                    @echo off & SETLOCAL & Title CDPR
                    
                    SET "sProgram=Cdpr.exe"
                    SET "sResults=2xUsers.txt"
                    IF NOT exist "%sProgram%" goto :ERROR
                    
                    :: start new logfile
                    >"%sResults%" (
                       echo.** %date% - %time% - %computername% - %username% **
                       echo.)
                    
                    :ask for user input
                    echo.Please enter the number of the NIC:
                    echo.
                    for /f "tokens=*" %%* in (
                       'echo.^|Cdpr.exe^|findstr /brc:"[0-9]*\. "'
                       ) do echo.%%*& >>"%sResults%" call:NICS %%*
                    echo.
                    Set /p "n=Card selection: "
                    
                    :nummeric input
                    Set /a "n=%n%"
                    If %n% EQU 0 cls & goto :ask again
                    
                    :: get user's choice
                    for /f "tokens=*" %%* in (
                       'echo.^|Cdpr.exe^|findstr /brc:"%n%*\. "'
                       ) do Set "choice=%%*"
                    echo.Selected card : %choice:~3%
                    echo.
                    echo.
                    echo.please wait....
                    
                    :: write to log
                    >>"%sResults%" (
                       echo.
                       echo.Selected card: %n%. "%choice:~3%"
                       echo.)
                    for /f "tokens=*" %%* in (
                       'echo.%n%^|Cdpr.exe /v^|findstr /bic:"value:"'
                       ) do call:write %%* >>"%sResults%"
                    
                    endlocal
                    
                    goto:EOF
                    :ERROR
                       echo There was an error in the path...
                       goto:eof
                    
                    :write
                    echo.%*
                    exit /b 0
                    
                    :NICS
                    echo. %*
                    exit /b 0
                    I am not sure though whether redirecting the output of the Cdpr.exe command will work or not.

                    /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


                    • #11
                      Re: Allowing Input within For Loop

                      Thanks! Yeah it only logs up to selecting the NIC card...but nothing after that. Closer though

                      Comment


                      • #12
                        Re: Allowing Input within For Loop

                        ** Cleaned up a little bit...if anyone has any better suggestions let me know.

                        Found another method of accomplishing the same thing that cdpr.exe does, using tcpdump (finding the port ID and cisco switch that the NIC is plugged into). I probably should have specified this in case anyone already knew of this other method.

                        The blog link is here: http://blog.wallis2000.co.uk/

                        There is also a vbscript method posted in this blog, but I didn't look too much into it. Below is the batch file using tcpdump that works for me, its a little ugly though:

                        Code:
                        @echo off
                        
                        SET "sOUT=c:\out.txt"
                        SET "sResults=C:\cdpr\tcpdump_trial_license\results.txt"
                        
                        tcpdump -D
                        set Interface=
                        echo.
                        set /P Interface=Enter Interface Number: %=%
                        tcpdump -nn -v -s 1500 -i %Interface% -c 1 ether[20:2] == 0x2000 >%sOUT%
                        >>%sResults% (
                           echo.-------------------------------------------------------------------------------
                           echo.** %date% - %time% - %computername% - %username% **
                           echo.)
                        type %sOUT% |findstr /R "\Device-ID \Address.[(] \Management.Addresses \Port-ID" >>%sResults%
                        del %sOUT% >nul
                        I tried cleaning it up to specify path variables, but then stopped working. Not sure why but this was the only way it worked for me.
                        Last edited by ekrengel; 25th May 2011, 16:28. Reason: Cleaned up code a little bit

                        Comment

                        Working...
                        X