Changeset 7530
- Timestamp:
- Feb 25, 2010, 6:27:14 PM (15 years ago)
- Location:
- 2.0/nanobsd/nanobsd/files/usr/local
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified 2.0/nanobsd/nanobsd/files/usr/local/etc/rc.d/sshtun ¶
r7469 r7530 1 1 #!/bin/sh 2 # 3 # PROVIDE: sshtun 4 # REQUIRE: DAEMON 5 # KEYWORD: shutdown 2 6 # 3 7 # Add the following line to /etc/rc.conf to enable ssh-tun: 4 8 # 5 9 # sshtun_enable="YES" 6 # 7 # PROVIDE: sshtun 8 # REQUIRE: LOGIN 9 10 # 10 11 . /etc/rc.subr 11 12 … … 13 14 rcvar=`set_rcvar` 14 15 15 flags=${sshtun_flags} 16 16 command_interpreter=/bin/sh 17 17 command=/usr/local/sshtun/sshtun.sh 18 18 19 pidfile=/var/run/${name}.pid 19 command_args="${sshtun_options} &" 20 20 21 21 load_rc_config ${name} … … 23 23 sshtun_enable=${sshtun_enable-"NO"} 24 24 25 stop_cmd="sshtun_stop" 26 start_cmd="sshtun_start" 27 28 sshtun_start() 29 { 30 ${command} start ${flags} & 31 } 32 33 sshtun_stop() 34 { 35 ${command} stop 36 } 37 25 sshtun_pidfile=${sshtun_pidfile-"/var/run/sshtun.pid"} 26 pidfile="${sshtun_pidfile}" 38 27 39 28 run_rc_command "$1" 40 29 41 -
TabularUnified 2.0/nanobsd/nanobsd/files/usr/local/sshtun/sshtun.sh ¶
r7472 r7530 1 1 #!/bin/sh 2 # Simple deamon which does remote connecting to a sertain host for tunnel 3 # forwarding 4 # 5 # Created by Richard van Mansom - Jan 2010 6 # Make deamon friendly by Rick van der Zwet - Feb 2010 7 2 8 3 9 # Define some vars 4 DIR=`dirname $0` 5 CMD=$1 10 PWD=`dirname $0` 11 SSH_FLAGS=$@ 12 6 13 HOST="sshtun.wirelessleiden.nl" 7 REMOTEPORT=$28 LOCALPORT="22"9 14 USER="ssh-tun" 10 SSHKEY="${DIR}/sshkey" 11 WAIT="10" 15 SSHKEY="${PWD}/sshkey" 16 PID='/var/run/sshtun.pid' 17 LOGFILE='/var/log/sshtun.log' 18 19 # Program internal variables 20 WAIT="10" 12 21 REMOTEWAIT=`expr 3600 \* 24 \* 365` 13 echo ${REMOTEPORT} 22 23 log() { 24 echo `date "+%b %e %T"` ": " $* >> ${LOGFILE} 25 } 26 27 # Store script PID in right location 28 echo $$ > $PID || exit 1 29 30 SSHPIDFILE=`mktemp -t $(basename $0)` 31 32 trap_exit() { 33 if [ -s $SSHPID ]; then 34 kill `cat $SSHPID` 35 rm $SSHPID 36 fi 37 exit 38 } 39 40 # Don't leave the ssh client process behind 41 trap "trap_exit" 0 1 2 15 14 42 15 43 # Make sure a port is specified 16 if [ -n "$REMOTEPORT" ]; then 44 if [ -z "$SSH_FLAGS" ]; then 45 echo "Usage: $0 <SSH_CLIENT_FLAGS>" 46 exit 64 47 fi 17 48 18 case $1 in 19 start) 20 21 PROCESS=`ps -ax | grep localhost | grep sshtun | grep ${REMOTEPORT} | grep -v grep | awk '{print $1}'` 22 if [ -z "$PROCESS" ]; then 23 24 sleep 1 49 # Main program 50 log "[INFO] Connecting with args: $SSH_FLAGS" 25 51 26 echo "Connecting using port $REMOTEPORT" 27 28 # Make sure the script never dies 29 while [ true ] 30 do 31 32 # Connect to remote site 33 ssh -R ${REMOTEPORT}:localhost:${LOCALPORT} -i ${SSHKEY} ${USER}@${HOST} -o ServerAliveInterval=5 -o ExitOnForwardFailure=yes -o BatchMode=yes -o StrictHostKeyChecking=no -n -N "sh -c 'sleep ${REMOTEWAIT}'" 34 # Echo some stuff 35 date 36 echo "Sleeping $WAIT seconds" 37 echo "" 38 39 # Go to sleep 40 sleep ${WAIT} 41 done 42 43 else 44 echo "Tunnel already running" 45 echo "Pid: " 46 echo ${PROCESS} 47 fi 48 49 ;; 50 stop) 51 52 # Killing the tunnel 53 PROCESS=`ps -ax | grep -v stop | grep -E '(ssh-tun|sshtun)' | grep ${REMOTEPORT} | grep -v grep | awk '{print $1}'` 54 55 if [ -n "$PROCESS" ]; then 56 echo "Killing session with port $REMOTEPORT : 57 $PROCESS" 58 echo ${PROCESS} | xargs kill -9 59 fi 60 ;; 61 62 *) 63 # Display usage info 64 echo "Usage: ./sshtun <ACTION> <PORT>" 65 ;; 66 esac 67 fi 52 # Make sure the tunnel keeps on reconnecting 53 while true 54 do 55 # Connect to remote site 56 ssh ${SSH_FLAGS} -i ${SSHKEY} ${USER}@${HOST} -o ServerAliveInterval=5 \ 57 -o ExitOnForwardFailure=yes -o BatchMode=yes -o StrictHostKeyChecking=no \ 58 -n -N "sh -c 'sleep ${REMOTEWAIT}'" & 59 60 # Save the sshtun PID 61 SSHPID=$! 62 echo ${SSHPID} > ${SSHPIDFILE} 63 log "[NOTICE] Used PID: $SSHPID" 64 65 # As long the client is running make sure to sleep and relax, don't make it very 66 # long as a sleeping shell script cannot receive sigtals 67 while [ `ps ax | awk '{ print $1 }' | grep "^$SSHPID\$"` ]; do 68 sleep 10 69 done 70 log "[NOTICE] SSHTUN Disconnected ${SSHPID}" 71 72 # Mark process disconnected 73 echo "" > ${SSHPIDFILE} 74 75 log "[INFO] Sleeping $WAIT seconds before reconnect" 76 sleep ${WAIT} 77 done 78
Note:
See TracChangeset
for help on using the changeset viewer.