0

I want to run some process that requires an opened ssh tunnel.

How do you run some command AFTER the ssh tunnel is successfully opened? Timers are not good enough as network speed and remote machine load might heavily affect the time needed to open up the connection...

--UPDATE-- I want to open the tunnel and run the command on the local machine

xvga
  • 103
  • 1
  • 4

2 Answers2

1

You can use LocalCommand ssh option, but there are some limitations. From man ssh_config:

LocalCommand

Specifies a command to execute on the local machine after successfully connecting
to the server.  The command string extends to the end of the line, and is
executed with the user's shell.  The following escape character substitutions
will be performed:
  ‘%d’ (local user's home directory),
  ‘%h’ (remote host name), ‘%l’ (local host name),
  ‘%n’ (host name as provided on the command line),
  ‘%p’ (remote port),
  ‘%r’ (remote user name) or ‘%u’ (local user name).

The command is run synchronously and does not have access to the session of
the ssh(1) that spawned it. It should not be used for interactive commands.

This directive is ignored unless PermitLocalCommand has been enabled.

Here is quick example:

ssh -v -o PermitLocalCommand=yes -o 'LocalCommand=/bin/date >/tmp/test' localhost

Later, you probably need to put those options into proper Host section in $HOME/.ssh/config file.

kupson
  • 3,578
  • 20
  • 19
0

ssh -fN will cause the SSH client to background itself once the connection is established, so you should be able to just wait for that and then execute the command.

mgorven
  • 30,615
  • 7
  • 79
  • 122