Scripting a ssh tunnel
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:
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:
Remember that you might want to backup the value of the $! macro before running the program, because it could get overwritten.
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.
|
|
Kickstarting the PyS60 bluetooth console on Ubuntu |
|
|
Fighting for privacy? |
Comments
There are no comments for this post