Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have two OpenSolaris servers. The remote server has a MySQL database. MySQL is configured with skip-networking; so, I can't access that database from the first server by simply using the hostname and port.

I must have an ssh tunnel setup to connect to MySQL from the local server. I can do this with a simple : ssh -L 3350:localhost:3306 user@server and SSH keys. It works great.

However, I need the connection to be permanent and preferably run on bootup as well.

I've created a little bash script to create the connection. However, it simply connects, ends the script, and loses the ssh connection. If I modify the script to run a command on the remote server and and a sleep command, it will stay connected for X seconds. I can then connect with MySQL. However, I will surely lose the connection after X seconds.

Does anyone have suggestions for a more elegant way to do this? Is there someway to use Solaris' svcadm to create the connection and maintain it at all times?

UPDATE : I've discovered that if I add sleep X and then create the MySQL connection before X expires, the ssh connection stays up indefinitely. However, that is not a very stable solution. If I lose the database connection, the ssh connection dies and then I'll be stuck without being able to connect again.

share|improve this question

5 Answers 5

up vote 6 down vote accepted

You want the -N option to OpenSSH. For example:

ssh -N -L 3350:localhost:3306 user@server &
share|improve this answer
    
LOL! I never thought of that. I was looking right at it with the man page. Thanks for the quick answer –  Justin Jun 12 '09 at 4:20
3  
A nicer options is to use "ssh -T -f -N -L 3350:localhost:3306 user@server", SSH won't use a tty and immediately go into background. For the options to work you'll need public key authentication as you won't have any chance of entering a password –  Server Horror Jun 12 '09 at 7:16

You want to look at a tool like autossh, which keeps an ssh tunnel working. If it dies, it automatically recreates it.

share|improve this answer
    
You could also add the command to inittab so the init process would start it on boot and restart it if ti stops (which would be due to error). –  David Spillett Jun 12 '09 at 8:24
    
I can't install autossh or rstunnel. Check out jaisenmathai.com/blog/2008/10/10/… for a great way to maintain the connection permanently. –  Justin Jun 17 '09 at 2:51

I think you can try ssh-copy-id - permits easy propagation of SSH pub/priv keypair

ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.75.10
share|improve this answer

Or maybe rstunnel, which does almost the same as autossh -> http://sourceforge.net/projects/rstunnel/

share|improve this answer

If you want to avoid the overhead of ssh, encryption, authentication, and such, you can do something very similar with netcat (or nc). There are lot of tutorials on the web. If you start with them and need more specific help, let me know.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.