Can ssh_config’s ProxyCommand run a local command before connecting to a remote machine?

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 <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="543931142631393b20317a3935373c3d3a31">[email protected]</a>

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 <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="452820053720282a31206b2824262d2c2b20">[email protected]</a>

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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)


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x