Ssh tunnel

From Attie's Wiki
Jump to: navigation, search

You can use this setup to 'call home' from a machine that is behind a firewall.
Once the tunnel is active, you can connect to ${TUNNEL_HOST}:${TUNNEL_PORT} via localhost:${LISTEN_PORT} (only on ${REMOTE_HOST}).

~${OWNER}/.ssh/config

Host tunnel
  User ${REMOTE_USER}
  Hostname ${REMOTE_HOST}
  IdentityFile ${REMOTE_IDENTITY}
  ServerAliveInterval 300
  ServerAliveCountMax 2
  GatewayPorts no
  RemoteForward ${LISTEN_PORT} ${TUNNEL_HOST}:${TUNNEL_PORT}
  ExitOnForwardFailure yes

/etc/init.d/tunnel_${OWNER}

#!/bin/sh
 
set -e
 
OWNER=${OWNER}
 
NAME=tunnel_${OWNER}
PIDFILE=/var/run/${NAME}.pid
DAEMON=/usr/bin/ssh
DAEMON_OPTS="tunnel"
 
export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
 
function start {
  echo -n "Starting daemon: ${NAME}"
  start-stop-daemon --start --quiet --pidfile ${PIDFILE} --user ${OWNER} --chuid ${OWNER} -bm --exec ${DAEMON} -- ${DAEMON_OPTS} || ( echo " [FAILED]"; exit 1 )
  echo " [OK]"
}
 
function status {
  echo -n "Status of daemon: ${NAME}"
  start-stop-daemon --status --pidfile ${PIDFILE} --user ${OWNER} -v || ( echo " - not running"; exit 1 )
  echo " - running"
}
 
function stop {
  echo -n "Stopping daemon: ${NAME}"
  start-stop-daemon --stop --quiet --pidfile ${PIDFILE} 2>/dev/null || ( echo " [FAILED]"; exit 1 )
  echo " [OK]"
}
 
function restart {
  echo "Restarting daemon: ${NAME}..."
  stop
  start
}
 
case "$1" in
  start)
    start
  ;;
  status)
    status
  ;;
  stop)
    stop
  ;;
  restart)
    restart
  ;;
 
  *)
    echo "Usage: "$1" {start|stop|restart}"
    exit 1
esac
 
exit 0

Post-Setup

update-rc.d tunnel_${OWNER} defaults
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox