Custom Scripts

We now have an interface that will connect into the ISP automatically and hang up after a predefined time period. This is great, although some users prefer to control the dialup and timeout processes themselves. To do this, we'll create a few short scripts: startPPP to initialize the connection and keep it open, and stopPPP to terminate the link.

Let's first look at how we start it:

cesium# cat startPPP
#!/bin/ksh
#
# script to initiate the PPP link
#
remoteMachine="remote-ppp"  # the remote entry in hosts
sleepTimer=250              # must be less than ppp timeout
pidFile=/tmp/startPPP.pid

#
# check PPP is not already running
#
if [ -f ${pidFile} ]; then
  thePid=$(cat ${pidFile})
  pidStat=$(ps –p ${thePid} | grep –v PID | wc –l | awk '{ print $1 }')
  if [ ${pidStat} –gt 0 ]; then
       echo "Error: startPid is  already running"
    exit 1
  else
    rm ${pidFile}
  fi
fi
#
# start the ppp daemon
#
/etc/init.d/asppp start
#
# initiate the connection
#
ping ${remoteMachine} > /dev/null
pingableMachine=$(grep nameserver /etc/resolv.conf | head -1 |
   cut -d' ' -f 2)
ping ${pingableMachine} 300 > /dev/null
if [ $? –ne 0 ]; then
  echo "Error: Cannot connect to ${pingableMachine}"
  exit 1
fi
echo "$$" > ${pidFile}
#
# now keep it open by looping around indefinitely
#
while true; do
  ping ${pingableMachine} 60 > /dev/null
  sleep ${sleepTimer}
done
exit 0
cesium#

To open the connection, startPPP first checks to see if PPP is already established. If not, it will ping the remote machine to establish the connection, then will loop around pinging another accessible machine every so often to ensure the PPP inactivity time-out is never met. It also writes its process ID to a file to easily allow us to find the process later to stop it.

We can see that we use two different addresses for ping to check. This is because of the dummy address problem, where we need to ping the dummy address to initiate the connection, but once it is made the dummy address is exchanged for the real address. We need to use an address we know is external to keep the connection alive. We have done this in the script by picking the first nameserver entry out of /etc/resolv.conf.

Now, let's look at how the connection is closed:

cesium# cat stopPPP
#!/bin/ksh
#
# script to stop the PPP connection
#
pidFile=/tmp/startPPP.pid

if [ -f ${pidFile} ]; then
  /etc/init.d/asppp stop
  kill $(cat ${pidFile})
  if [ $? –eq 0 ]; then
    echo "PPP connection terminated"
    rm ${pidFile}
  else
    echo "Error: Cannot terminate the connection"
    exit 1
  fi
else
  echo "PPP is not connected"
  exit 2
fi
exit 0
cesium#

To stop the process, the script checks for the existence of the file that contains the Process Identifier (PID) and, if there, stops the link manager. Next it kills off the ping process to the remote machine, and finally cleans up by removing the PID file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset