LJ Archive

Sophisticated ppp-on Script With chat Options

  #!/bin/sh
  #
  # ppp-on -- start a PPP session, with any other necessary actions

  # exit status
  sts=0

  # setup default script location and file extensions
  if [ -z $ppppth ]; then
    # note: don't quote ppppth; otherwise shell expansions
    # may not work quite right.
    ppppth=~/.ppp
  fi
  if [ -z $diaxtn ]; then
    diaxtn="chat"
  fi
  if [ -z $pppxtn ]; then
    pppxtn="ppp"
  fi

  # help function
  function prthlp()
  {
    echo ""
    echo "usage: $0 [-v] [-c] [<name-to-dial>]"
    echo ""
    echo "open ppp link. if <name-to-dial> is specified, use a 'chat' script"
    echo "called $ppppth/<name-to-dial>.$diaxtn to make the dialup "
    echo "connection; otherwise, we expect the dialup connection to already "
    echo "be made. see chat(1) for format of chat scripts."
    echo ""
    echo "options:"
    echo "-v verbose operation; logs chat to system log."
    echo "-c connect script; perform script after ppp connected. "
    echo " if <name-to-dial> is specified, perform "
    echo " $ppppth/<name-to-dial>.$pppxtn, otherwise"
    echo " perform $ppppth/default.$pppxtn."
    echo ""
  }

  # option defaults
  # -v => verbose chat operation
  opt_vrbopr=""
  # yes => perform connect script
  opt_scpflg="no"

  # various options
  for i in $*; do
    case $i in
      -v | --verbose)
        opt_vrbopr="-v"
        shift
        ;;
      -c | --connect)
        opt_scpflg="yes"
        shift
        ;;
      -h | --help)
        prthlp
        exit 0
        ;;
      *) break
        ;;
    esac
  done

  # if this script is run with an argument, use that
  # as the basename for the dialing and connect scripts;
  # otherwise, use default values.
  if [ -z $1 ]; then
    diaflg="no"
    diacmd=""
    cnnscp="$ppppth/default.$pppxtn"
  else
    diaflg="yes"
    diascp="$ppppth/$1.$diaxtn"
    diacmd="chat $opt_vrbopr -f $diascp"
    cnnscp="$ppppth/$1.$pppxtn"
  fi

  # delay to allow for connection
  cnndly="5s"

  # connection device and speed
  pppdev=/dev/modem
  pppspd=38400

  # this is the command we use to start the ppp daemon
  pppcmd="pppd $pppdev $pppspd"

  # this function starts up ppp
  function pppstu()
  {
    if $pppcmd; then
      # sleep for a little bit to give ppp time to connect
      sleep $cnndly
      echo "ppp connected."
      if [ $opt_scpflg = "yes" ]; then
        (
        # perform user- or connection-specific actions
        # upon successful ppp connection
        if [ -x $cnnscp ]; then
          sleep $cnndly
          $cnnscp
        fi
        ) &
      fi
    else
      echo "error: ppp not connected."
      sts=1
    fi
  }

  # dial if requested and then start pppd
  echo "opening ppp connection over $pppdev ... "
  if [ "$diaflg" = "yes" ]; then
    # we're dialing first, then starting ppp
    echo -n "dialing $1 ... "
    if $diacmd <$pppdev >$pppdev; then
      echo "connected."
      pppstu
    else
      echo -e "\nerror: dialing failed."
      sts=1
    fi
  else
    # we're not dialing; just start ppp
    pppstu
  fi

  exit $sts
LJ Archive