allan.simon
allan.simon

Reputation: 4316

Bash - Know if a SSH tunnel has been correctly created

Here's the situation

I have my computer, named MY_COMPUTER a machine named REMOTE_VM on which I have a user remoteuser that has in /etc/passwd the shell /bin/false

This user has only one function, doing a SSH tunnel to be able to do after a reverse SSH.

the command I currently use is

ssh \
   -nNT \
   -R 42242:127.0.0.1:22 \
   remoteuser@REMOTE_VM

I would like this command to be executed in a script and to have a way to get a callback or a way (like putting this ssh command in background and do a periodic check on the existence of a socket file or something like that) to check that the connection has been actually opened and not simply pending connection in the case of a slow network.

Is there such a way with the openssh-client or you have to go with a custom implementation of a ssh client so that you can put your callback whereever you want ?

I would prefer as much as possible a (even dirty) solution with plain openssh-client and bash.

Upvotes: 2

Views: 431

Answers (1)

bishop
bishop

Reputation: 39434

Run the command verbosely, capturing the output, then test that the output indicates the session is established? Along the lines of:

TMPFILE=$(mktemp)
2>"$TMPFILE" ssh -v -nNT -R 42242:127.0.0.1:22 remoteuser@REMOTE_VM &
grep 'debug1: Entering interactive session.' "$TMPFILE"

Alternatively, perhaps you could poll the output of netstat.

Upvotes: 1

Related Questions