-2

I'd like to have a one-liner for this. When I use the following command:

ssh -o ProxyCommand='ssh -W %h:%p user@<jump server>' -t user@<rails server> 'sudo su another_user ; cd /some/dir ; RAILS_ENV=production bundle exec rails console'

I end up on the rails server, but I'm not in the rails console:

$(rails server)

I then try to exit, thinking it didn't work:

$(rails server) exit
exit

However, it locks and looks like above. Then I hit CTRL-C and I get a ruby exception. I'm not posting the ruby exception because I don't think it's relevant and it's different every time. It seems like it's interrupting the rails console, but the rails console never appears to me.

Matt Lins
  • 3
  • 3
  • Ok, I'm really asking more about how to properly use SSH and felt I'd get more expertise here rather than on stackoverflow. – Matt Lins Oct 22 '15 at 16:06
  • Make that clearer in your question. Tell us what you did and how it didn't work. – user9517 Oct 22 '15 at 17:56

1 Answers1

3

Your problem problem appears to be in this part of the command

sudo su another_user ; cd /some/dir ; RAILS_ENV=production bundle exec rails console

This invokes three commands after each other. Each one of them will wait for completion of the previous command.

The first command sudo su another_user will start a shell as another_user. Once you leave that shell, the following commands will be executed.

The commands cd /some/dir ; RAILS_ENV=production bundle exec rails console will be run as user who you logged in as. If you expected those commands to run as another_user, and they don't behave as you intended, when they are run as user, then that will explain your problems.

Perhaps you meant to write

sudo su another_user -c "cd /some/dir ; RAILS_ENV=production bundle exec rails console"

or

sudo su - another_user -c "cd /some/dir ; RAILS_ENV=production bundle exec rails console"

where the extra - causes environment variables to be updated and login scripts to be run as another_user before the rest of the command is executed.

Answer which I wrote for a previous revision of the question

For better security and ease of use, I recommend you run both ssh commands on the client host and none on the jump server. The combination of ProxyCommand and -W arguments is perfectly suited for that.

ssh -o ProxyCommand='ssh -W %h:%p jump-server' -t app-server 'cd /some/dir ; rails console'

The -t argument and command should cover the rest of your requirements.

kasperd
  • 30,455
  • 17
  • 76
  • 124
  • I actually got this far on my own, but it doesn't quite work. Basically the command finishes with me at the command prompt on the app server. The weird thing is, when I 'exit' it locks up. Then I ctrl-c and I get a ruby exception. Any ideas on that? – Matt Lins Oct 22 '15 at 16:07
  • @MattLins I don't know how rails is supposed to behave. But interactive programs will behave weird if you forget the `-t`. Other than that the only functional difference I would expect between typing it as a single command and as a sequence of commands would be which environment variables are set. – kasperd Oct 22 '15 at 18:28