0

I would like to ssh onto a remote machine, automatise some code execution with heredoc and then provide the user with a bash prompt to manually execute further commands as needed.

So far I tried (option 1)

ssh user@host << 'EOF'
echo "Hello"
EOF

This prints out "Hello" just fine, but closes the ssh connection and exits the script. Also I get the infamous "Pseudo-terminal will not be allocated because stdin is not a terminal."

Same result with a single -t switch.

Next I tried (option 2)

ssh -tt user@host << 'EOF'
echo "Hello `uname -n`"
EOF

Which does the job, but produces a set of weird outputs:

Success. Logging you in...
echo "Hello"
echo "Hello"
[remote]$ echo "Hello"
Hello
[remote]$ <

Tried without using heredoc, like

ssh user@host 'echo "Hello"; /bin/bash' 

but the echo will be executed on a different shell

So far, option 2 got me the closest. Any other solutions would be welcome!

Éva
  • 1
  • 1
  • 1
    Maybe something like `ssh -t user@host <<< "commands; exec bash"` will work. Cannot try at the moment. – Socowi Jul 21 '21 at 10:59
  • See [Run Remote ssh command with Full Login Shell](https://superuser.com/q/306530/652023). – Socowi Jul 21 '21 at 11:26

1 Answers1

1

You could try setting some ssh options on the command line. These can also be set in the user's ~/.ssh/config file. Replace remote-host with user@host or equivalent ssh config Host pattern.

single line

ssh -o RequestTTY='yes' -o RemoteCommand='echo "Hello $(uname -n)"; bash -l' remote-host

multiple lines

ssh -o RequestTTY='yes' -o RemoteCommand='
echo "hello from $(uname -n)"
echo "I am $(whoami)"
bash -l
' remote-host
Cole Tierney
  • 9,571
  • 1
  • 27
  • 35