suggestions/review of a ssh script

suggestions/review of a ssh script

Post by par » Mon, 23 Jul 2001 05:35:20



hi,

i created a script to transfer 110/pop3 connection which grew to what
you see below. now it includes ftp, news, telnet port tranferrence and
regualr ssh connection.

can anybody comment on the usage function, killing process, and
anything else?

it was written on/for freebsd 4.3, but should be usable elsewhere.

thanks much.

- parv

=================  portssh  
#!/bin/sh

##  use PATH only what's required...
##    cat, test, ssh
#
PATH=/bin:/usr/bin

##  set server for regual ssh connection & for port transfer
#
REMOTE_ssh_server=

## set for transferring port & used to kill 'transferred' connections
#
REMOTE_xfer_host=

#  --  be sure you know what you are doing before editing below ---

# ssh options
#  2: use ssh v2 protocol
#  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 -a -C -f -N -x'
OPT_ssh='-2 -a -C -x'

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] [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] [nntp|119])
_USAGE_

Quote:}

# assign service based on script name (in case of sym link), or use $1
#
case $1 in
  ftp|21)
  SERVICE=ftp

  LOCAL_xfer_port=410021
  REMOTE_xfer_port=21
  ;;

  telnet|23)
  SERVICE=telnet

  LOCAL_xfer_port=410023
  REMOTE_xfer_port=23
  ;;

  pop3|110)
  SERVICE=pop3

  LOCAL_xfer_port=410110
  REMOTE_xfer_port=110
  ;;

  news|nntp|119)
  SERVICE=nntp

  LOCAL_xfer_port=410119
  REMOTE_xfer_port=119
  ;;

  ssh|22)
  SERVICE=ssh

  shift
  ;;

  stop|0)
  SERVICE=stop
  shift
  ;;

  *)
  usage
  exit
  ;;
esac

#
##  stop, kill actually, ssh clients
#
if test "$SERVICE" = 'stop'
then

  # get ps output & convert spaces to #'s
  #
  # line wrapped only for posting
  #
  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')

    # lines wrapped for posting
    #
    case $1 in
      ftp)
      Pid=$( echo $current_ps | \
            awk '/410021:'"$REMOTE_xfer_host"':21/ {print $1}' )
      ;;

      telnet)
      Pid=$( echo $current_ps | \
            awk '/410023:'"$REMOTE_xfer_host"':23/ {print $1}' )
      ;;

      pop3)
      Pid=$( echo $current_ps | \
            awk '/410110:'"$REMOTE_xfer_host"':110/ {print $1}' )
      ;;

      nntp)
      Pid=$( echo $current_ps | \
            awk '/410119:'"$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 -SIGKILL $Pid

        echo ' ... done'
    fi
  done

  exit

#
##  transfer local port
#
elif test "$SERVICE" != 'ssh'
then
  # create tunnel/forward port only
  #
  # lines wrapped for posting
  #
  echo ' -' $SERVICE: localhost $LOCAL_xfer_port '->' $REMOTE_xfer_host \
       $REMOTE_xfer_port
  echo '  ' via $REMOTE_ssh_server ...

  ssh  $OPT_xfer \
       -L ${LOCAL_xfer_port}:${REMOTE_xfer_host}:${REMOTE_xfer_port} \
       $REMOTE_ssh_server "sleep 40"

  echo '  ...done w/ return code' $? '(0: success, else failure)'

#
##  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


fi

# end of script
exit
=================

--
 so, do you like word games or scrabble?
         - parv