#!/bin/bash # /etc/hosts and /root/.ssh/config must be edited for this to work: # TARGET is the computer to which you are tunneling # put an alias $HOSTHAME-tunnel on the same /etc/hosts line as $TARGET # example: # 10.10.0.100 myserver.my mylaptop-tunnel # # under Host $HOSTNAME-tunnel in /root/.ssh/config # optionally change Port, User, and/or IdentityFile # example: # Host mylaptop-tunnel # IdentityFile /home/somebody/.ssh/id_dsa_special # User somebody # Port 9876 # # no need to set -o StrictHostKeyChecking=no with scp when using this method # but test the script before putting in cron, to cache the host key DEBUGGING=1 # set to 0 for less verbose output if ! scp -q /etc/profile $HOSTNAME-tunnel:/tmp/; then exec 3< <(lsof -n -P -itcp -R) while read -u 3 command pid ppid ignored; do if [ "$DEBUGGING" ]; then echo FOUND: $command $pid $ppid >&2 fi if [ "$command" = "ssh" -a "$ppid" = 1 ]; then if [ "$DEBUGGING" ]; then echo Killing tunnel process $pid >&2 fi kill $pid fi done fi