0

I have two remote server and i am trying to execute more than one command on a remote host to which i am connected by using ssh command. My command syntax is like:

ssh -t -i key-1 user1@remote-1 "ssh -t -i key-2 user2@remote-2 "cmd-1;cmd-2;...cmd-n;"";

I have tried using semi-colon (;) and && symbols between two commands and observed that first command executes on remote-2 and second command executes on remote-1.

My requirement is that I want to execute all commands except the last one on remote-2. And, the last command on remote-1.

Note: I know how to execute multiple commands by connecting to single remote host. So, I will appreciate if answer is given only after understanding the problem statement.

Shalem
  • 1,446
  • 2
  • 22
  • 47
  • I assume, you got 2 hosts/remote servers and some set of commands.. You are trying to execute all except the last command on remote2 and the last command on remote1. Am i correct? – Shalem Nov 25 '19 at 14:09
  • Yes. And please note that my requirement is that I shall execute all commands at one go. I cannot break these commands into sequence of commands. – Shitanshu Lall Nov 25 '19 at 14:22
  • @Shalem: I know that last command can be executed on remote-1 if I use semi-colon just before my last command. – Shitanshu Lall Nov 25 '19 at 14:30
  • Take a look at the `ProxyJump` and `ProxyCommand` options in `man ssh_config` to eliminate the need to run `ssh` explicitly on the jump host. Properly configured, you'll be able to simply run `ssh remote-2 "cmd-1; cmd-2; ... cmd-n"`, and your configuration will take care of connection to `remote-2` via `remote-1`. – chepner Nov 25 '19 at 15:23
  • Just for clarification, do you mean `ssh -t -i key-2 ....` as the first command and `cmd-1;cmd-2;....cmd-n;` as the last command, or do you just mean `cmd-1` as the first command and `cmd-n` as the last command? You've got a lot of nested things that could be construed as commands which makes it a bit difficult to answer with something that will help! – th3ant Nov 25 '19 at 15:23
  • @th3ant - Your second assumption is correct. – Shitanshu Lall Nov 25 '19 at 23:47
  • @chepner - Thank you for inputs. I will try this and get back with results. – Shitanshu Lall Nov 25 '19 at 23:47
  • Part of the problem is that you appear to be trying to nest double quotes. Try `ssh ... "ssh -t ... \"cmd-1; cmd-2; ...cmd-n\""` (which would run the `n` commands on remote-2). – chepner Nov 26 '19 at 01:20

2 Answers2

0

Assuming that you cannot create config files to simplify your command on the two hosts, and that you do not want to split this into two separate commands, it should be as simple as moving the last command out of the inner SSH command:

ssh -t -i key-1 user1@remote-1 "ssh -t -i key-2 user2@remote-2 \"cmd-1;cmd-2;...cmd-n-1\"; cmd-n"

# or

ssh -t -i key-1 user1@remote-1 'ssh -t -i key-2 user2@remote-2 "cmd-1;cmd-2;...cmd-n-1"; cmd-n'

You should also escape your double nested quotes, or simply change your outer/inner quotes to single quotes providing you are not expanding within this command.


Also as an aside, you can simplify your SSH command greatly by using a .ssh_config file, specifically with the ProxyJump parameter (man page).

th3ant
  • 328
  • 1
  • 9
0

Instead of single-line, consider taking advantage of here-documents to simplify the sequence. It has the advantage of making it easier to enter long commands.

ssh -t -i key-1 user1@remote-1 <<__END__
    ssh -t -i key-2 user2@remote-2 "cmd-1;cmd-2;..."
    cmd-n
__END__

I'm not able to test locally, but should be possible to nest further

ssh -t -i key-1 user1@remote-1 <<__END__
    ssh -t -i key-2 user2@remote-2 <<__SUB__
        # Execute on remote2
        cmd-1
        cmd-2
        ...
__SUB__
    # Execute on remote1
    cmd-n
__END__
dash-o
  • 13,723
  • 1
  • 10
  • 37