How can I run a command at remote Host2 via remote Host1, while adding my key to the ssh-agent, so that the passphrase prompt does not interfere with the latter part of the script?
I am basically trying to run a command at remote Host2 via remote Host1, without the passphrase prompt interfering. (The local machine cannot ssh into remote Host2 directly). [SSH has been setup to use key authentication.]
Although the end result is ultimately to pipe a file, I imagine a more general description is still relevant.
If I simply nest the commands like so:
ssh -t Host1 "ssh Host2 "cat remotefile.mp4""
I am prompted for the passphrase of Host1 since the key is not present in the agent there.
If I wish to pipe the output of that command from Host2 to another process in the local machine, the passphrase prompt also gets piped or redirected as it is in the way.
I thus consider doing the following, to get Hosts1's key into the agent before running the relevant command:
ssh -t Host1 "`eval ssh-agent -s`; ssh-add ~/.ssh/host1-id_key; ssh-add -l; ssh Host2 "cat remotefile.mp4""
However I am having trouble with the proper syntax chaining/nesting the above mentioned commands in bash.
I get:
syntax error near unexpected token `;;'
I believe it has to do with the backticks command substitution syntax, and I admit I don't completely understand its implications.
I have tried the following variations but with the same failed results:
ssh -t Host1 "`eval ssh-agent -s` && ssh-add ~/.ssh/host1-id_key && ssh-add -l; ssh Host2 "cat remotefile.mp4""
ssh -t Host1 << HERE
`eval ssh-agent -s`;
ssh-add ~/.ssh/host1-id_key;
ssh-add -l;
ssh Host2 "cat remotefile.mp4"
HERE
How do I successfully chain/nest several commands to be executed by the remote shells, in particular the eval ssh-agent one.