11
ssh -t remotehost vim /tmp/x.txt

I know that I can run a command like the above.

But I would like to be able to run any local bash code in a remote machine. For this reason, I'd like to call the remote 'bash -s' so that can process any local bash code.

ssh -t remotehost 'bash -s' <<< vim /tmp/x.txt

However, the above example shows "Pseudo-terminal will not be allocated because stdin is not a terminal." Is there any way to let ssh take local bash code via stdin and run it via the remote 'bash -s'? Thanks.

user1424739
  • 11,937
  • 17
  • 63
  • 152

2 Answers2

11
ssh -t remotehost 'bash -s' <<< vim /tmp/x.txt

You're getting the "Pseudo-terminal will not be allocated..." message because you're running ssh with a single -t option, when the standard input to the ssh process isn't a TTY. ssh prints that message specifically in this case. The documentation for -t says:

-t
Force pseudo-terminal allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.

The -t command-line option is related to the ssh configuration option RequestTTY:

RequestTTY
Specifies whether to request a pseudo-tty for the session. The argument may be one of: no (never request a TTY), yes (always request a TTY when standard input is a TTY), force (always request a TTY) or auto (request a TTY when opening a login session). This option mirrors the -t and -T flags for ssh(1).

A single -t is equivalent to "RequestTTY yes", while two of them is equivalent to "RequestTTY force".

If you want your remote command(s) to run with a TTY, then specify -t twice:

ssh -tt remotehost 'bash -s' <<< vim /tmp/x.txt
or
ssh -t -t remotehost 'bash -s' <<< vim /tmp/x.txt

ssh will allocate a TTY for the remote system and it won't print that message.

If the command(s) being run on the remote system don't require a TTY, you can leave the -t option out:

ssh remotehost 'bash -s' <<< vim /tmp/x.txt
Kenster
  • 23,465
  • 21
  • 80
  • 106
1

I believe the following might suit your purposes:

vim /tmp/x.txt ; ssh remotehost 'bash -s' < /tmp/x.txt

The first expression (vim ...) allows you to specify the commands you want to execute remotely as a local file called /tmp/x.txt; the second expression (ssh ...) calls the remote bash interpreter, and sends the contents of your local file to it for execution. Note that you do not need the -t option for ssh in this case (which gave rise to the pseudo-terminal warning), and that you do not need to use a here string (<<<) but can use the normal file input operator (<).

This solution seems to work for, e.g., the following file contents:

echo These commands are being executed on $HOSTNAME
echo This is a second command
ngj
  • 883
  • 7
  • 17
  • I forgot to mention you can remove the `-t` option from the `ssh` command in this case, which should get rid of the pseudo-terminal warning. I edited the answer above; the current command now seems to work correctly on my system (it executes the local file remotely without showing the warning). – ngj Feb 06 '18 at 17:53
  • If this answer does not solve your question, consider clarifying the actual commands you want to execute remotely. – ngj Feb 06 '18 at 19:00