-1

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:

GavinD
  • 25
  • 8
  • 2
    `for /F "delims=" %%a in ('echo %string%^| C:\my_folder\plink.exe 192.168.0.112 -P 6950 -raw -batch') do set "output=%%a"` – Aacini Aug 15 '20 at 12:33
  • As you've already identified that you want the third token, all you need to do is to select the line. I'd suggest that the simplest way to do that is to use `For /F "EOL=+ Tokens=3" …`, which should exclude any lines which begin with the **`+`** character. – Compo Aug 15 '20 at 18:37
  • I would have preferred that you made a comment to my answer, before using it as an edit to your question. How about you first take a look at what my example code does, that can be easily done, by removing the appropriate **`@`** character(s). If you change the for loop to read, (1st example): `For /F "Delims=" %%G In ('Echo !string!') Do @Set "output=%%G"`, (2nd example): `For /F "Delims=" %%G In ('Echo %string%') Do @Set "output=%%G"`, you should see what command is running and the `%output%` content, should be `GET VALUE `, if it is then the carets have done their job perfectly. – Compo Aug 16 '20 at 12:11
  • Once you've done that, can you explain to us what exactly happens when you run this directly in the Command Prompt window, `Echo GET VALUE ^|"C:\my_folder\plink.exe" 192.168.0.112 -P 6950 -raw -batch`. I'd also be interested to know what is wrong with accepting input from a file? and why, when you did so, you didn't use the `-m` option over input redirection **`0<`**? – Compo Aug 16 '20 at 12:15
  • I had already studied your suggestion and removed the @ characters to see what was going on. That’s how I discovered that the carets were causing the issue in the string! The reason for wanting to use a variable as inputs and outputs is because I am polling a server to monitor the state, every second. This generates unnecessary disk activity. It's not a major issue but I feel that having the inputs and outputs as variables is a much better solution than using files. I have tried your suggestions and added my findings to the question. – GavinD Aug 16 '20 at 13:18
  • Well you were wrong then, weren't you? It is clear that my two examples of escaping both create exactly the correct string within your for loop command. The issue is that piping the string to your command is not accepted, hence the error messages. So to recap you cannot pipe the string to the plink command, as initially suggested in the opening comment by @Aacini. I would suggest that you use the `-m` option, which works and is therefore necessary disk activity. – Compo Aug 16 '20 at 18:55
  • OK, thanks for your efforts anyway. It had me scratching my head for hours hence why I was asking on here. So what was I wrong about then? Incidentally, I don't see any comments from @Aacini, only the code. – GavinD Aug 16 '20 at 19:32
  • It was no effort for me, Aacini provided a command you stated that the output was three lines and you wanted the result to be the third token of the second line. I provided the code to retrieve the second line only and the third space/tab delimited token on it. Had you stated that there wasn't three lines of output resulting from Aacini's commented code, I wouldn't have supplied my answer, to retrieve something from output which isn't being returned. *When I provided it in a comment, you incorrectly stated that it doesn't work because I had not correctly escaped the string, even though I had.* – Compo Aug 16 '20 at 19:42

1 Answers1

1

It looks as if you need to escape the < and > characters with carets, ^.

If you want to use delayed expansion, you only need to escape them when you define the variable:

@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

If you don't want to use delayed expansion, you'll need to double escape, which needs three carets:

@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
Compo
  • 36,585
  • 5
  • 27
  • 39