Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Before I can to connect to a particular remote machine I have to run a certain local command. So instead of ssh me@remote.machine I have to do

local_command
ssh me@remote.machine

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 me@remote.machine

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

share|improve this question
add comment

2 Answers 2

up vote 6 down vote accepted

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

share|improve this answer
    
This works perfectly, thanks a lot. –  Dave Jul 31 '12 at 6:03
add comment

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)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.