11

Before I can to connect to a particular remote machine I have to run a certain local command. So instead of ssh [email protected] I have to do

local_command
ssh [email protected]

I would like to automate this so that I only have to do ssh remote.machine.

I know that I can achieve this at the shell level by creating my own ssh script that calls /usr/bin/ssh, but can I do it using the ProxyCommand option of ssh_config?

As far as I understand it, I need something like

Host remote.machine
    ProxyCommand local_command; ssh [email protected]

in my ~/.ssh/config file, but not exactly this of course because it's circular!

2 Answers 2

13

If using ProxyCommand, you must use something like /usr/bin/nc to connect the server.

For invoking your command before connect, you need to use sh -c "command list" to merge the two commands as one.

Host remote.machine
    ProxyCommand sh -c "local_command; /usr/bin/nc %h %p"

MORE:

If your local_command is too complicated, you can use a script:

cat my_connect.sh
#!/bin/bash

local_command
/usr/bin/nc "$1" "$2"

The ssh config becomes:

Host remote.machine
        ProxyCommand /path_to_my_connect.sh %h %p

At the last, you can add your own proxy to the /usr/bin/nc

1
1

Use the example from the page you linked:

ProxyCommand /usr/bin/nc -X connect -x 192.0.2.1:8080 %h %p

Invokes nc (netcat) to connect to a proxy (proxy running on 192.0.2.1:8080) and passes %h and %p to the ssh command (%h = hostname, %p = port)

You must log in to answer this question.

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