Home > Enterprise >  Multi-lined command output set as variable
Multi-lined command output set as variable

Time:09-27

So I've googled and searched stackflow; I think the output for the library I'm using and the way it's structured makes none of the answers work.

I downloaded a windows library called totp2 for command prompt. I am executing the command in my batch file and that works fine, but it wants to return the line I passed.

I'm looking to run the command, and only assign the very last line to a variable; stripping the word OTP: from the last line.

C:\Users\Dan\Desktop\devBash> t2otp.exe QQRZO4QIGPJFXS2QXDIL4JUO2KKQCPTOF2D5AX5M3R2VH5NM2ZUSXIXGP4WPMVTR sha256 6 30

T2OTP v2 - Command line TOTP Generator (c) TOKEN2
Algorithm: sha256
Seed: QQRZO4QIGPJFXS2QXDIL4JUO2KKQCPTOF2D5AX5M3R2VH5NM2ZUSXIXGP4WPMVTR
Interval: 30 seconds
OTP length: 6 digits
    
OTP: 666323

The above is exactly how it returns the command prompt results, with the code being on line 7 of the command output.

Current Code (hardcoded command just for testing):

For /F "EOL=/" %%A In ('t2otp.exe QQRZO4QIGPJFXS2QXDIL4JUO2KKQCPTOF2D5AX5M3R2VH5NM2ZUSXIXGP4WPMVTR sha256 6 30') Do Set "totp=%%A"

However that returns just OTP:

Current Result:

OTP: 666323

Needed Result:

666323

Any help is really really appreciated.

And yes; it's a demo secret.

CodePudding user response:

For /F "Tokens=2 eol=/" %%A In ('t2otp.exe QQRZO4QIGPJFXS2QXDIL4JUO2KKQCPTOF2D5AX5M3R2VH5NM2ZUSXIXGP4WPMVTR sha256 6 30') Do Set "hash=%%A"

Easy way to feel dumb is when the answer is so simple. Solved.

CodePudding user response:

An even simpler answer is to add "clean" parameter at the end of the command (as described in Token2's documentation):

  • the last argument is optional, if argument equals to 'loop' , the tool will stay launched recalculating the current OTP until stopped with Ctrl C, if the argument is 'clean' the tool will produce the OTP only

So the code below:

t2otp.exe QQRZO4QIGPJFXS2QXDIL4JUO2KKQCPTOF2D5AX5M3R2VH5NM2ZUSXIXGP4WPMVTR sha256 6 30 clean

Will give you only the OTP (6 digits in your example)

  • Related