#!/bin/sh # Sets up a tunnel from the local host A to a remote host B and rewrites # .ssh/config on B to be able to access A from B. This is useful if, for # instance, A is a laptop hosting some services via SSH. These services # will be accessible via the same hostname, even if A moves from place to # place (no need to modify svn+ssh URL's and so on). # If $HOST is hostname.tld, the .ssh/config on B should have: # Host hostname.tld # HostKeyAlias hostname.tld # Hostname anything # Port any-number # The Hostname and Port lines will be rewritten by this script. # Arguments: # $1: remote host B. # $2: free port on B used for the tunnel. if [ $# -ne 2 ]; then echo "Usage: $0 " >&2 exit 1 fi ssh -f -N -R ${2}:localhost:22 $1 ssh $1 "perl -pi -e '/^Host\s+(.*)/ and \$host = \$1; \$host eq \"$HOST\" or next; s/^(\s*Hostname)\s.*/\1 localhost/; s/^(\s*Port)\s.*/\1 $2/' .ssh/config" # $Id: ssh-forward 38408 2010-08-03 17:11:27Z vinc17/xvii $