Is there any way to force SSH to use a particular shell on the remote end, regardless of what the user's default shell is?
I've tried solutions akin to:
ssh host.domain.com /bin/bash -c 'complicated, multi-line command'
but unfortunately the default shell on the remote end is responsible for parsing the "complicated, multi-line command" part, and I'm having difficulty escaping it sufficiently to work both for Bash and C shell users.
You can use the
-t
option to force allocation of a pseudo-tty for the program you want to start, as if you were executing the standard shell. Then pass the shell you want as a plain old argument.With this technique you're able to not only use any shell that's installed but you can also open vim and other programs which require a TTY, from a single command. Which is cool if you're writing a shell script that logs you in somewhere and opens a file on vim, or htop or something.
Here's
bash
sh
works too. As does anything else really.Not sure whether this is a login shell but there's options to make bash act like a login shell, so your shell might have that too.
Use a heredoc:
I don't believe this is possible, at least with openssh-based systems. If you have the ability, a better solution might be to sftp up a shell-script file, and then execute it with the method you posted. It would have the advantage of minimizing the amount of escaping needed, but would leave a file behind that would have to be removed (perhaps as the last step of the script).
Use key-based logins, not password-based. Then you can add a (list of) "forced command(s)" to your public ssh key (in the "options" field in case of SSH1) which is installed on the server (in ~/.ssh/authorized_keys file for SSH1, ~/.ssh2/authorization for SSH2).
Make your forced command so that your desired shell is called...
More: You can associate at most one forced command to a given key. If you require multiple forced commands for different purposes, you have to setup different keys. (Of course you can put multiple things into one script, which you call via forced command. But be aware that forced commands are always run for a given account/key if the user logs in, regardless if he asked for something different to run. If you want to still honor the original command asked for, have a look into how to exploit the
$SSH_ORIGINAL_COMMAND
variable...)Read up about "forced commands" via Google.
Surprisingly I see different results with the following:
run in dash:
vs bash:
Showing the fully quoted command is working as expected.
I faced a similar situation a while back where I needed to use ksh coprocess for sqlplus and I only had an ssh through which reads and writes must take place.
A way to do this is to pipe all your dependent command into one line (use ;) to /usr/bin/ksh on the remote machine. for example: