How to use sshpass for chained connection?

I have two customer's machines, $gateway and $server, the second one is only accessible from the first one. Public key auth is unavailable on both systems. So, to execute some $command on the server I need to run ssh $gateway ssh $server $command and then type two passwords. I can use sshpass, but it will handle only the first connection and I still need to enter the second password by hand.

How can I make completely noninteractive (without manually entering passwords) ssh access to the server?

The only way I see is to use tunnels, but they are hard to keep alive because of unstable internet connection.


Solution 1:

Try:

ssh -oProxyCommand="ssh -W %h:%p $gateway" $server command

For example:

ssh -oProxyCommand="ssh -W %h:%p [email protected]" [email protected] command

This will use proxy the ssh connection to $server over another ssh command, without actually creating a tunnel. This makes sure that both ssh clients run locally, thereby using sshpass.