#!/bin/sh ## author: parv, parv UNDERSCORE AT yahoo DOT com ## date: dec 24 2001 ## ## license: free to use as you please w/ proper credit given ## ## name: myssh ## ## purpose: wrap ssh port transfer options and plain ssh ipv4 ## connections ## ## usage: to see usage, run... ## myssh ## ## use PATH only for what's required... # ssh (of course) # cat, echo, test # awk, sed, (e)grep # ps, kill # PATH=/bin:/usr/bin ## set server for regual ssh connection & for port transfer # REMOTE_ssh_server=remote.host ## set for transferring port & used to kill 'transferred' connections # REMOTE_xfer_host=remote.host # --- be sure you know what you are doing before editing below --- # ssh options # 2: use ssh v2 protocol # 4: force ipv4 address # 6: force ipv6 address # a: disable auhentication agent forwarding # C: use compression/gzip # f: go to background after authentication # L: forward local port # N: don't execute remote commands (just port forwarding; ssh2 only) # v: be verbose # x: disable X11 forwarding # X: allow X11 forwording # OPT_xfer='-2 -4 -a -x -c blowfish -f -N' OPT_ssh='-2 -4 -a -x -c blowfish' # OPT_xfer="$OPT_xfer -C" OPT_ssh="$OPT_ssh -C" # #OPT_xfer="$OPT_xfer -v -v -v" #OPT_ssh="$OPT_ssh -v -v -v" # usage () { cat <<_USAGE_ # see ssh man page for various options given below. # # befor starting, make sure that you have set your own... # REMOTE_ssh_server -- destination server to connect to # REMOTE_xfer_host -- reciever of the transferred host - to transfer local port to remote port, give one of the options... `basename $0` ([ftp|21] [telnet|23] [pop3|110] [imap|143] [nntp|119]) default options for port transfer: $OPT_xfer - to start ssh... `basename $0` [ssh|22] default options for ssh: $OPT_ssh - to stop/kill port transfer processes only... a. kill all of them... `basename $0` [stop|0] b. kill cretain ones, specify type... `basename $0` [stop|0] ([ftp|21] [telnet|23] [pop3|110] [imap|143] [nntp|119]) _USAGE_ } # assign service based on 1st argument given # case $1 in ftp|21) SERVICE=ftp LOCAL_xfer_port=50021 REMOTE_xfer_port=21 ;; telnet|23) SERVICE=telnet LOCAL_xfer_port=50023 REMOTE_xfer_port=23 ;; pop3|110) SERVICE=pop3 LOCAL_xfer_port=50110 REMOTE_xfer_port=110 ;; imap|143) SERVICE=imap LOCAL_xfer_port=50143 REMOTE_xfer_port=143 ;; news|nntp|119) SERVICE=nntp LOCAL_xfer_port=50119 REMOTE_xfer_port=119 ;; ssh|22) SERVICE=ssh # shift so that rest of $@ can be used as "remote command" shift ;; stop|0) SERVICE=stop shift ;; *) usage exit ;; esac ## kill function, used before starting a service or explicit request # kill_me () { # get ps output & convert spaces to #'s # PS=$(ps -wax | egrep "ssh.* -L .*:${REMOTE_xfer_host}:.*" | sed 's! !#!g' | egrep -v 'ps -wax|grep|sed') # no process found, exit then # if test -z "$PS" then echo ' -' no ssh process found, exiting... exit 0 fi # otherwise, kill 'em # for current_ps in $PS do # restore spaces current_ps=$(echo $current_ps | sed 's!#! !g') case $1 in ftp) Pid=$(echo $current_ps | awk '/50021:'"$REMOTE_xfer_host"':21/ {print $1}') ;; telnet) Pid=$(echo $current_ps | awk '/50023:'"$REMOTE_xfer_host"':23/ {print $1}') ;; pop3) Pid=$(echo $current_ps | awk '/50110:'"$REMOTE_xfer_host"':110/ {print $1}') ;; nntp) Pid=$(echo $current_ps | awk '/50119:'"$REMOTE_xfer_host"':119/ {print $1}') ;; *) Pid=$(echo $current_ps | awk '{print $1}') ;; esac if test -z $Pid then echo ' -' no ssh process found ${1} else # for debugging only... # #echo $current_ps echo ' -' $1 killing $Pid ... kill -SIGTERM $Pid echo ' ... done' fi done } # ## stop, kill actually, ssh clients # if test "$SERVICE" = 'stop' then kill_me $1 exit # ## transfer local port # elif test "$SERVICE" != 'ssh' then # kill old functions before starting... # #kill_me # create tunnel/forward port only # echo ' -' ${SERVICE} xfer: localhost $LOCAL_xfer_port '->' $REMOTE_xfer_host $REMOTE_xfer_port echo ' ' destination: $REMOTE_ssh_server ... echo ssh $OPT_xfer -L ${LOCAL_xfer_port}:${REMOTE_xfer_host}:${REMOTE_xfer_port} $REMOTE_ssh_server rc=$? [ $rc -eq 0 ] && echo && echo ' ' ...connection M A D E [ $rc -ne 0 ] && echo && echo ' ' ...connection F A I L E D "(rc: ${rc})" exit $rc # ## start ssh # else # by chance, we manage to specify a ssh connection, either start # ssh or display/exit; i decided to start ssh # #echo ' -' ssh: $REMOTE_ssh_server exec ssh $OPT_ssh $REMOTE_ssh_server "$@" fi exit # end of script