Scripting a ssh tunnel

By Ghost on Saturday 09 August 2008 11:42
Category: Linux, Views: 2426

Sometimes I wonder why I put up with some inconveniences in my work flow. One particular issue I have for a long time is working with ssh-tunnels. It always goes like this: I start a ssh tunnel or other proxy, run a program that uses this proxy and use it until I am done with it, and finally close the proxy afterwards. For a long time I was doing this manually using a terminal, until I finally got fed up with the tedious routine and wrote this simple bash script:

code:
1
2
3
4
#!/bin/sh
/usr/bin/ssh -N -D 3124 username@localhost &
# Enter your program here
kill $!


There are a few precautions, however. Firstly, you have to be able to log in to the ssh server using a key. This is because you cannot supply ssh with a password from the command line (easily). So you have to be sure you log in automatically when you enter the command. For more information about setting up ssh keys, you can refer to this howto.

Secondly, you must prevent the script from continuing after running your own command. Ordinarily, you do not have to worry about that, although some applications (some gui programs like nautilus, for instance) will detach itself from the foreground. You have to find out the process id of the program and insert the following command before the kill sequence:

code:
1
wait <pid>

Remember that you might want to backup the value of the $! macro before running the program, because it could get overwritten.

Volgende: Kickstarting the PyS60 bluetooth console on Ubuntu 11-08
Volgende: Fighting for privacy? 15-07

Comments

There are no comments for this post


Comment form
(required)
(required, but will not be displayed)
(optional)

Please enter the characters you see in the image below: