WP-CLI alias: connect with ssh proxy

I have a wp-cli.yml file that defines an alias for a remote host:

@origin:
  ssh: myserver.example.com
  path: /path/to/htdocs

This would normally allow me to use wp @origin command to run remote commands on the other host. In one case, however, the remote host is only visible through an SSH proxy, so my system has an SSH config entry in ~/.ssh/config:

Host myserver.example.com
    ProxyCommand ssh -q -W %h:22 proxyserver.example.com
    ForwardAgent yes

This instructs it to first connect to the proxy server, then connect from there to destination server. I can log into that box in a single step simply by typing:

$ ssh myserver.example.com

However, when I attempt the same with WP-CLI it doesn’t work:

$ wp @origin db export --add-drop-table -
Error: Cannot connect over SSH using provided configuration.

I conclude that WP-CLI either doesn’t look at my SSH config or doesn’t understand the ProxyCommand directive there.

Is there anything I can add to either the wp-cli.yml config or the wp command to make it use a proxy the same way SSH does?

1 Answer
1

I created simillar configuration to yours and proxy is working for me. My config looks like:

Host production
  HostName xxx.xxx.xxx.xxx
  ProxyCommand ssh -q -W %h:22 vagrant
  ForwardAgent yes

Host vagrant
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /path/to/.vagrant/machines/default/virtualbox/private_key
  IdentitiesOnly yes
  ForwardAgent yes
  LogLevel FATAL

I’m treating my vagrant local machine as proxy and I want to execute command on production server.

My wp-cli.yml file looks like:

@local:
  ssh: vagrant/var/www/test

@prod:
  ssh: production/var/www/test

I’m executing wp command and in return I get data from my production server.

wp @prod config get

I know that proxy is working because if I remove Host vagrant section from ~/.ssh/config file I can’t execute any command and in return I get an error.

You can try install wp-cli localy using composer

composer require wp-cli/wp-cli

echo escaped command variable and execute command like that:

./vendor/bin/wp @prod config get

This variable should contain your full ssh command. Maybe this will give you a hint what is going on.

Leave a Comment