Search code examples
sshcygwintunnel

Cygrunsrv & autossh : A way to embedd remote commands in the command line?


I'm using cygrunsrv and autossh on windows XP to create a service building a tunnel to a remote server but i also want to create another tunnel from the remote server to another server.

I can achieve it with this command line :

autossh -M 5432 serverA -t 'autossh -M 4321 serverB -N'

but when I want to set it up in cygwin through cygrunsrv to make it works as a service :

cygrunsrv -I TUNNEL -p /usr/bin/autossh -a "-M 5432 serverA -t 'autossh -M 4321 serverB -N'" -e AUTOSSH_NTSERVICE=yes -e AUTOSSH_POLL=20 -e AUTOSSH_GATETIME=30

It's not fully working. The service is creating the tunnel correctly to ServerA but it's not sending the autossh command "autossh -M 4321 serverB -N" to ServerA. I tried to escape the quote but all my efforts didn't make any difference and I'm not seeing any command sent in the autossh logs.

I think the problem is related to pseudo terminal that is not created through the cygrunsrv.

I'd like to know if there's a way to fix my cygrunsrv command line to make it work or should I consider a different approach ?


Solution

  • Lionel, try removing the AUTOSSH_NTSERVICE=yes from the cygrunsrv invocation. As /usr/share/doc/autossh/README.Cygwin explains:

    Setting AUTOSSH_NTSERVICE=yes in the calling environment ... change[s] autossh's behavior in three useful ways:

    (1) Add an -N flag to each invocation of ssh, thus disabling shell access. The idea is that if you're running autossh as a system service, you're using it to forward ports; it wouldn't make sense to run a shell session as a system service. (If you think this reasoning is wrong, please send a bug report to the author or Cygwin maintainer, and tell us what you're trying to do.)

    Despite what the above says, it seems that you may have a good reason for not wanting -N (which suppresses command execution) in your service's ssh invocation. Removing AUTOSSH_NTSERVICE=yes should take care of it. It will have a couple of other minor disadvantages, but you can probably live with it. Read the rest of README.Cygwin for the details.