1

I'm trying to write a batch file to run in Windows 10 Pro that will use Plink to establish an SSH session to a remote server and execute some commands. Everything works well, except for whatever reason I end up with extra line feeds with each ECHO command I pipe in. Normally, this isn't an issue, until the command I'm running requires some specific user feedback, namely, pressing Y to confirm an action. Since it receives the extra line feed after testing STSTest command and before receiving the Y character, it throws an error.

Here's my batch script:

set PATH=C:\Program Files\PuTTY;%PATH%
set TestNum=%1

(
    TIMEOUT /t 1 > nul
    ECHO cd /usr/bin/core/test
    ECHO rm STS_*.txt
    ECHO rm STS_T1_Test%TestNum%.txt
    ECHO ./STSTest --T 2 --i %TestNum%
    TIMEOUT /t 1 > nul
    ECHO Y
    TIMEOUT /t 1 > nul
    ECHO exit
) | plink -ssh 192.168.1.20 -l root -pw ***

Does anybody have an idea on how to eliminate that extra line feed so that Y is entered in the correct order after the STSTest command is entered?


Here's a simpler example demonstrating what I'm fighting. If I define this simple batch file:

(
    TIMEOUT /t 1 > nul
    ECHO cd /
    ECHO cd usr
    ECHO cd bin
    ECHO cd core
    ECHO cd test
    TIMEOUT /t 1 > nul
    ECHO exit
) | plink -ssh 192.168.1.20 -l root -pw ***

The results from the command window look like:

Last login: Wed Jul 29 23:53:30 2020 from 192.168.1.7
root@core-A:~# cd /
root@core-A:/#
root@core-A:/# cd usr
root@core-A:/usr#
root@core-A:/usr# cd bin
root@core-A:/usr/bin#
root@core-A:/usr/bin# cd core
root@core-A:/usr/bin/core#
root@core-A:/usr/bin/core# cd test
root@core-A:/usr/bin/core/test#
root@core-A:/usr/bin/core/test# exit

I get an extra line feed after every ECHO command.

0

3 Answers 3

1

When you are executing plink without any command on its command-line, plink starts an interactive terminal session. That has lot of unwanted side effects. Including those that you face.

Add -T switch to plink command line to avoid that:

(
...
) | plink -T -ssh 192.168.1.20 -l root -pw ***

Another option is doing everything on the server-side and specifying full command on the plink commandline (in this case, you will also need to add the -batch switch):

plink -ssh 192.168.1.20 -l root -pw *** -batch "cd /usr/bin/core/test && rm STS_*.txt && rm STS_T1_Test%TestNum%.txt && (echo Y | ./STSTest --T 2 --i %TestNum%)"

Or some combination of both these methods.


Similar questions:

1
  • With the first option I get errors from the device I'm connecting to "-sh: line 1: cd: too many arguments". With the second option, the batch file runs smoothly, but I did have to add the -batch option to the plink call "plink -ssh -batch 192.168.1.20 -l root -pw" Commented Dec 15, 2021 at 13:37
0

A batch file echo uses the Windows line ending (carriage return+line feed).

Combine the newline hack with the no line ending hack:

@echo off
REM don't remove blank lines below
set NLM=^


set NL=^^^%NLM%%NLM%^%NLM%%NLM%

(
    <nul set /P=foo%NL%
    <nul set /P=foo%NL%bar%NL%
) > test.txt

and test.txt becomes (hex output)

66 6F 6F 0A 66 6F 6F 0A 62 61 72 0A     foo.foo.bar.
4
  • Thanks for the reply. I tried that, and unfortunately it seems like it is removing all new line characters. The session enters the "./STSTest --T 2 --i %TestNum%" line, and sits there until the 3 seconds times out and the "ECHO Y" line executes, which then adds "Y" and a new line to the end of the first line, and I miss the response window. Commented Dec 14, 2021 at 18:27
  • Then you should try to explain better what you need. Each ECHO ends with a newline unless you use a hack...
    – Anders
    Commented Dec 14, 2021 at 19:03
  • I updated my original post with the issue I am encountering. It's like the ECHO command is sending two different line feeds every time it's invoked. Commented Dec 14, 2021 at 19:11
  • I tried an example I found like this earlier and it didn't work. I figured I would try again by copy/pasting your example, and I get the same results. The command session hangs when I try to use the %NL% variable. However, I think I have figured out a way past it with Martin's help. Thank you for your replies. Commented Dec 14, 2021 at 21:58
0

In case anyone is looking for a solution for appending remote file using plink with Powershell and to exclude the carriage return

&$plink -ssh -agent -batch $sender  echo $string '| tee -a' $destination 

Not the answer you're looking for? Browse other questions tagged or ask your own question.