3

Im trying to SSH to host #1, who has access to host #2, and on host #2 I want to run a command. All from my local machine.

This bit works fine:

    ssh host1 -t "command; ssh user@host2"

But, on host2, I also want to cd to a directory and then stay connected

I tried using this:

    ssh host1 -t "command; ssh user@host2 -t 'command; cd /some/path/'"

The output I get is:

    Connection to host2 closed.
    Connection to host1 closed.

And ends up on my local machine

The expected result was my session being on host2 in the /some/path/ directory thru host1

2 Answers 2

7

Instead of tmux or screen as suggested by @heemayl, one can also use bash:

ssh host1 -t "command; ssh user@host2 -t 'command; cd /some/path/; bash'"
2
  • 1
    Thats a good one as far as the OP's requirement is concerned. +1.
    – heemayl
    Feb 12, 2015 at 10:53
  • This worked perfectly, and doesnt require additional packages on host2 as @heemayl suggested. Thank you! Feb 12, 2015 at 11:47
3

Install screen or tmux in "host2" then running the following will keep the session alive:

ssh host1 -t "command; ssh user@host2 -t 'command; cd /some/path/; screen'"

In case of you command i.e. ssh host1 -t "command; ssh user@host2 -t 'command; cd /some/path/'", the session will be closed after running the commands. But if you use screen or tmux the remote session will be kept alive.

Check the manpages of screen and tmux for details. tmux is the newer approach to solve the same problem screen solves.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .