What I want is a reverse ssh tunnel that starts up when my computer boots and doggedly sticks around come hell or high water. This allows me to connect to my computer from anywhere, even if my computer happens to wind up behind some wonky router that's out of my control.
So I emerged net-misc/autossh and wrote this initscript:
Code: Select all
#!/sbin/runscript
# Copyright 2011 Chad Joan
# Distributed under the terms of the GNU General Public License v2
depend() {
use net dns logger
after dns
}
start() {
ebegin "Starting autossh"
env \
AUTOSSH_GATETIME=0 \
AUTOSSH_FIRST_POLL=10 \
AUTOSSH_POLL=60 \
AUTOSSH_PIDFILE=/var/run/autossh.pid \
AUTOSSH_LOGLEVEL=7 \
AUTOSSH_DEBUG=1 \
start-stop-daemon --start \
--make-pidfile --pidfile /var/run/autossh.pid \
--exec /usr/bin/autossh \
-- -M29001 -f -N -R 1337:localhost:22 youruser@the_go_between.com
eend $? "Failed to start autossh"
}
stop() {
ebegin "Stopping autossh"
# Grab the child PID so we can kill it once autossh is down.
SSHPID=`ps -o pid= --ppid \`cat /var/run/autossh.pid\``
start-stop-daemon --stop \
--pidfile /var/run/autossh.pid \
--signal 9 \
--exec /usr/bin/autossh
kill $SSHPID
eend $? "Failed to stop autossh"
}
There are certainly flaws, notably that I didn't bother moving any of it into /etc/conf.d/autossh.init
Usage (untested, sorry) :
- Make sure your root user is has ssh private/public key authentication for the computer used as a go-between. This is needed to prevent the tunnel script from being asked for a password by your go-between computer; the initscript can't deal with that. You can do this as root like so:
Code: Select all
su
ssh-keygen
ssh-copy-id youruser@the_go_between.com
(Enter your password and check up on things afterwards.)
- Create the /etc/init.d/autossh.init initscript.
- `rc-update add autossh.init default`
- `sudo /etc/init.d/autossh.init start` or reboot your computer.
- `ssh youruser@the_go_between.com` (run this from any computer anywhere)
- `ssh -p 1337 someuser@your_home_computer.com` (run this while logged into the go-between computer)

