Schedule automatic SSH tunnel establishment to VMs

fatzopilot

New Member
Oct 6, 2011
22
0
1
Hi,

For administration purposes, I set up a SSH tunnel to tunnel postgres connections to their designated VM.
On login to the proxmox machine using putty, putty establishes a tunnel on a specific port from the remote machine as descriped here: http://www.postgresonline.com/journal/archives/38-PuTTY-for-SSH-Tunneling-to-PostgreSQL-Server.html. Autossh runs on the proxmox machine to "extend" that tunnel to a specific client. I.e. it is called from a script like this:
Code:
#! /bin/sh
#establish tunnel to postgres VM
autossh -M 0 -q -f -N -o "ServerAliveInterval 60" -o "ServerAliveCountMax 3" -L 5433:postgres.myserver.com:5432 -f postgres@postgres.myserver.com
Note that different tunnel ports are used to address individual postgres VMs.

I tried to create a service for it like this:
Code:
#! /bin/sh
### BEGIN INIT INFO
# Provides: establishSSHTunnels
# Required-Start: $ssh
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Establishes an SSH tunnel to the postgres VM
### END INIT INFO

NAME=establishSSHTunnels
DAEMON=/sbin/establishSSHTunnels

[ -x "$DAEMON" ] || exit 0

case "$1" in
start)
sleep 60 #let the DB VZ come up
exec /sbin/establishSSHTunnels
;;
stop|restart|force-reload|status)
#no op
;;
*)
echo "Usage: $NAME {start|stop|restart|force-reload}" >&2
exit 3
;;
esac

:

This works just fine, but the tunnel "extensions" are not established when the proxmox server boots up, i.e. the skript needs to be called manually or the service needs to be started manually, e.g. like this: service establishSSHTunnels start.

This is probably because of the delayed start of the VM which starts after proxmox.
I tried to remedy this using the "sleep 60" line but with no luck. Maybe there are also other reasons that prevent this approach from working.

Question is: Is there a way to schedule some work on the proxmox server after a specific VM has booted up?

Thanks
 
Just for those interested: It is actually much simpler to achieve a routing to the target maching :) Just replace localhost with the target machine name in the linked tutorial (e.g. dbinstance:5432) and you are done without needing autossh...
Disclaimer: I am using ssh certificates for login with the same authorized_keys2 file on all hosts, maybe it is more complicated without this...
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!