Changeset 8572
- Timestamp:
- Oct 16, 2010, 5:15:29 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
2.0/nanobsd/nanobsd/files/usr/local/sshtun/sshtun.sh
r8571 r8572 1 1 #!/bin/sh 2 # 2 3 # Simple deamon which does remote connecting to a sertain host for tunnel 3 4 # forwarding. … … 6 7 # !! !! 7 8 # !! THIS IS A HACK AND REQUIRES INTERACTIVE LOGIN ON THE REMOTE SIDE !! 8 # !! CANNOT BE USED WITHOUT ANY OTHER PROPER SECURITY MEASURES LIKE !! 9 # !! BUT LIMITED TO IP BLOCKING/USER LIMITED ACCESS AND PROPER LOGS. !! 9 # !! MAKE SURE TO LIMIT AND SECURE YOUR SETUP IN ANY WAYS YOU CAN !! 10 # !! INCLUDING BUT NOT LIMITED TO FIREWALL RULES, PROPER LOGGING, !! 11 # !! !! 12 # !! MAKE SURE TO SURE YOUR PUBLIC SSH KEY TO AVOID HARM BY USING: !! 13 # !! from="example.org",no-user-rc,no-X11-forwarding,no-pty, !! 14 # !! no-agent-forwarding,command="",permitopen="127.0.0.1:1" !! 10 15 # !! !! 11 16 # !!WARNING!!!!!!!!!!WARNING!!!!!!!!!!!WARNING!!!!!!!!!!WARNING!!!!!!!!!! 12 17 # 18 # Licence: BSDLike - https://www.wirelessleiden.nl/LICENSE 19 # 13 20 # Created by Richard van Mansom - Jan 2010 14 # Make deamon friendly by Rick van der Zwet - Feb 2010 21 # Make daemon friendly by Rick van der Zwet - Feb 2010 22 # Revamp to secure and reuse in multiple setups Rick van der Zwet - Oct 2010 15 23 24 # User variables 25 SSH_HOST=${SSH_HOST:-sshtun.wirelessleiden.nl} 26 SSH_USER=${SSH_USER:-ssh-tun} 27 SSH_KEY=${SSH_KEY:-${PWD}/sshkey} 16 28 17 # Define some vars 18 PWD=`dirname $0` 19 SSH_FLAGS=$@ 29 PIDFILE=${PIDFILE:-/var/run/sshtun.pid} 30 LOGFILE=${LOGFILE:-/var/log/sshtun.log} 20 31 21 HOST="sshtun.wirelessleiden.nl" 22 USER="ssh-tun" 23 SSHKEY="${PWD}/sshkey" 24 PID='/var/run/sshtun.pid' 25 LOGFILE='/var/log/sshtun.log' 26 27 # Program internal variables 28 WAIT="600" 29 REMOTEWAIT=`expr 3600 \* 24 \* 365` 30 31 # Make sure a port is specified 32 if [ -z "$SSH_FLAGS" ]; then 33 echo "Usage: $0 <SSH_CLIENT_FLAGS>" 34 exit 64 35 fi 32 RECONNECT_TIMEOUT=${RECONNECT_TIMEOUT:-`expr 60 \* 5`} 36 33 37 34 log() { … … 39 36 } 40 37 41 # Store script PID in right location 42 echo $$ > $PID || exit 1 38 # Internal helpers, storage and shortcuts 39 PWD=`dirname $0` 40 SSH_PID="" 43 41 44 SSHPIDFILE=`mktemp -t $(basename $0)` 45 42 # On (forced) exit, close tunnel and make sure to cleanup the leftovers. 46 43 trap_exit() { 47 if [ -s $SSHPID ]; then 48 kill `cat $SSHPID` 49 rm $SSHPID 44 if [ -n "$SSH_PID" ]; then 45 kill $SSH_PID 50 46 fi 51 exit47 rm -f $PIDFILE 52 48 } 53 49 54 # Don't leave the ssh client process behind 55 trap "trap_exit" 0 1 2 15 50 # Don't leave the ssh client process behind and exit with proper exit code 51 for SIGNAL in 1 2 15; do 52 trap "trap_exit; exit $SIGNAL" $SIGNAL 53 done 56 54 57 55 58 # Main program 56 # Make sure a port is specified 57 SSH_FLAGS=$@ 58 if [ -z "$SSH_FLAGS" ]; then 59 echo "Usage: $0 <SSH_CLIENT_FLAGS>" 60 echo "" 61 echo "Example: $0 -R 127.0.0.1:10022:127.0.0.1:22" 62 exit 64 63 fi 64 65 # Store script PID in right location 66 echo $$ > $PIDFILE || exit 1 67 59 68 log "[INFO] Connecting with args: $SSH_FLAGS" 60 69 … … 63 72 do 64 73 # Connect to remote site 65 ssh ${SSH_FLAGS} -i ${SSH KEY} ${USER}@${HOST} -o ServerAliveInterval=5 \74 ssh ${SSH_FLAGS} -i ${SSH_KEY} ${SSH_USER}@${SSH_HOST} -o ServerAliveInterval=5 \ 66 75 -o ExitOnForwardFailure=yes -o BatchMode=yes -o StrictHostKeyChecking=no \ 67 -n -N "sh -c 'sleep ${REMOTEWAIT}'"&76 -n -N 1>&2 2>> $LOGFILE & 68 77 69 78 # Save the sshtun PID 70 SSHPID=$! 71 echo ${SSHPID} > ${SSHPIDFILE} 72 log "[NOTICE] Used PID: $SSHPID" 79 SSH_PID=$! 80 log "[NOTICE] Used PID: $SSH_PID" 73 81 74 # As long the client is running make sure to sleep and relax, don't make it very 75 # long as a sleeping shell script cannot receive sigtals 76 while [ `ps ax | awk '{ print $1 }' | grep "^$SSHPID\$"` ]; do 77 sleep 10 78 done 79 log "[NOTICE] SSHTUN Disconnected ${SSHPID}" 82 # As long the client is running make sure to sleep and relax 83 wait 80 84 81 85 # Mark process disconnected 82 echo "" > ${SSHPIDFILE} 86 log "[NOTICE] SSHTUN Disconnected ${SSH_PID}" 87 SSH_PID="" 83 88 84 log "[INFO] Sleeping $ WAIT seconds before reconnect"85 sleep $ {WAIT}89 log "[INFO] Sleeping $RECONNECT_TIMEOUT seconds before reconnect" 90 sleep $RECONNECT_TIMEOUT 86 91 done 87 92 93 log "[ERROR] Should never be able to get here" 94 trap_exit 95
Note:
See TracChangeset
for help on using the changeset viewer.