28

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.

7

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).

  • This is what I eventually did, but using scp. A great idea. – plinehan Jul 21 '10 at 3:09
16

Use a heredoc:

ssh host.domain.com /bin/bash << EOF
big ugly commands
lots of them
EOF
  • shouldn't you use "-s" for bash to read commands from stdin? – Weboide Jul 20 '10 at 1:12
  • It isn't always required. – Ignacio Vazquez-Abrams Jul 20 '10 at 2:10
  • I would vote this down if I could because it prevents the commands from having access to stdin and the question was about invoking a particular shell. – Eric Woodruff Oct 6 '14 at 0:50
  • 2
    @EricWoodruff, ...invoking a particular shell (in this case bash) is exactly what this shows how to do. – Charles Duffy Apr 14 '17 at 1:10
  • FYI you can also do cat /tmp/tempfile_containing_your_script ssh ${hostname} /bin/bash. So instead of one step you have two steps: step 1 copy your script to a file, step 2 cat the script to ssh. – Trevor Boyd Smith May 17 '18 at 11:35
10

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.

  • Good stuff. That graphic on the O'Reilly page is very nice. In my particular case, however, I want to be able to force this for any users, not just users who have set up their keys correctly. I also don't have root on the server machines, so I can't edit files like /etc/sshrc. – plinehan Aug 2 '10 at 18:30
  • Well, it is always the server (or better: the one who exerts control over the server) who calls the shots when you connect to its service.... The 'owner' of the server decides what can be done with it. -- You cannot force anything 'for any user(s)' if you don't have higher privileges than them. – Kurt Pfeifle Aug 2 '10 at 20:25
  • If the client can run arbitrary commands then the client can run an arbitrary shell as a command too. – Eric Woodruff Oct 6 '14 at 0:48
  • @KurtPfeifle that link to O'Reilly is broken – Brian Vandenberg Mar 16 '15 at 20:05
  • @BrianVandenberg: Thx for the hint. I removed that link now. – Kurt Pfeifle Mar 16 '15 at 20:42
1

Surprisingly I see different results with the following:

run in dash:

ssh eric@172.17.1.241 /bin/bash -c "echo <(cat)"                                              
sh: 1: Syntax error: "(" unexpected

vs bash:

ssh eric@172.17.1.241 '/bin/bash -c "echo <(cat)"'                                            
/dev/fd/63

Showing the fully quoted command is working as expected.

  • 1
    Not surprising at all. The remote ssh daemon effectively runs sh -c "$*". Thus, you're running sh -c "/bin/bash -c echo <(cat)"; the echo command itself is the only argument passed to -c, and the <(cat) is a separate argument altogether. – Charles Duffy Apr 14 '17 at 1:10
0

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:

host="user@host"

db_conn="ora_user/passwd"

a="select * from dual;"

frmt="set heading off echo off feedback off verify off pagesize 0 termout off"

var=$(ssh ${host} "echo 'sqlplus -silent /nolog |&; sql_pid=\$!; print -p \"conn ${db_conn}\"; print -p \"${frmt}\"; print -p \"${a}\"; print -p \"exit\"; wait \$sql_pid' > /remote_dir/kshcmd.txt; awk '{print \$0}' /remote_dir/kshcmd.txt | /usr/bin/ksh")

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.