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.
CodePudding user response:
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:
- Windows batch scripting: SSH with Plink showing strange sequences in output
- Executing command using Plink does not work, but does in PuTTY
- Script via Plink in .bat behaves differently
CodePudding user response:
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