I have the following command line that I use in a batch file:
C:\my_folder\plink.exe 192.168.0.112 -P 6950 -raw -batch < C:\my_folder\string.txt > C:\my_folder\output.txt
string.txt
contains the string:
GET VALUE <PEP_LOGMODE>
The command line sends the string that is saved in string.txt
, to the server 192.168.0.112
. The server response is then saved in output.txt
.
The output should contain three lines of text, for example:
+Connected AppName="MyriadPlayout4" Version="4.0.28"
SET VALUE AUTO
+Success
This works but I am trying to find a way to use variables instead of files to send the string and to receive the response. More specifically, I am trying to capture the 3rd token on the second line, in this example, it is AUTO
. Unfortunately, I don’t think plink
supports variables as inputs or outputs so I think it may need some kind of command substitution or pipe, perhaps using For /f
, and usebackq
, but I am unable to work out the syntax.
UPDATE 1
I don’t necessarily need to use plink.exe. If anyone has a solution that works with netcat, nc, telnet of similar, please suggest that.
The suggestion from @Aacini is very close to the answer. However, I have had to enable:
SetLocal EnableDelayedExpansion
Because string contains the special characters <>
The variables are then defined using !var!
instead of %var%
This is what I have so far:
SetLocal EnableDelayedExpansion
SET "string=GET VALUE <PEP_LOGMODE>"
echo !string!
for /F "delims=" %%a in ('echo !string!^| C:\my_folder\plink.exe 192.168.0.112 -P 6950 -raw -batch') do set "output=%%a"
echo %output%
....but it now returns the error | was unexpected at this time.
When it tries to parse the string with for /f
Any help would be greatly appreciated.
UPDATE 2
The code provided by @Compo looks like it should do the trick:
@SetLocal EnableExtensions EnableDelayedExpansion
@Set "string=GET VALUE ^<PEP_LOGMODE^>"
@Set "output="
@For /F "EOL=+ Tokens=3" %%G In ('Echo !string! ^| "C:\my_folder\plink.exe" 192.168.0.112 -P 6950 -raw -batch') Do @Set "output=%%G"
@Set output 2> NUL
@Pause
and
@SetLocal EnableExtensions DisableDelayedExpansion
@Set "string=GET VALUE ^^^<PEP_LOGMODE^^^>"
@Set "output="
@For /F "EOL=+ Tokens=3" %%G In ('Echo %string% ^| "C:\my_folder\plink.exe" 192.168.0.112 -P 6950 -raw -batch') Do @Set "output=%%G"
@Set output 2> NUL
@Pause
... however, the carets in the string in both examples, are causing syntax errors when they try to parse the string with for /f
.
UPDATE 3
I have tried:
For /F "Delims=" %%G In ('Echo !string!') Do @Set "output=%%G"
This produces:
output=GET VALUE <PEP_LOGMODE>
So, it appears the input string is correct.
When I run:
Echo GET VALUE ^<PEP_LOGMODE^>|"C:\my_folder\plink.exe" 192.168.0.112 -P 6950 -raw -batch
….from the command line, it get:
The syntax of the command is incorrect.
+Connected AppName="MyriadPlayout4" Version="4.0.28"
This would indicate to me that the connection has been made to the server BUT the command is missing or incorrect.
I get the same response if I run:
C:\my_folder\plink.exe 192.168.0.112 -P 6950 -raw -batch
...but without the syntax error: