Issue
I would like to simulate this shell session:
$ ssh remotehost
[remote] $ cat > /tmp/test.txt
testing
newline
^D[remote] $ echo another cmd
another cmd
[remote] $ ^D
which will create a new file /tmp/test.txt
with the content "testing\nnewline\n", then run another command and terminate the ssh session.
I know that the ascii value for the EOT (end of transmission character) is 0x04
. So I thought I could emulate this by running:
$ echo "cat > /tmp/test.txt\ntesting\nnewline\n\x04echo another command\n" | ssh -tt remotehost
This seems to work in that it properly sends the commands, but it gets stuck when the end of file character should end the cat command. What am I doing wrong?
Solution
Thank you @dave_tompson_085. Your comment made me realize I should check timings, and use printf. The following command works:
(printf 'cat > /tmp/test.txt\n';
sleep 2;
printf 'testing\nnewline\n\x04';
sleep 1;
printf 'echo another command\n\x04') | ssh -tt ...
An alternative solution is to use a here doc in the call to the cat
command.
Answered By - Jason Eveleth Answer Checked By - Marie Seifert (WPSolving Admin)