Previous Next Table of Contents

14. Automating your connections - Creating the chat script

A chat script automates the log in and PPP start up so all you have to do (as root or as a member of the ppp group) is issue a single command to fire up your connection.

A chat script basically consists of expect send pairs. For example, once the modem has dialled and connected to the remote modem, we expect to receive a login prompt (possibly preceded by a lot of text - such as the /etc/issue file on the server machine). Once we have received this, we send our user name. Next, we expect the password prompt and send our password.

Quite simple in principle really.

If the ppp package installed correctly, you should have two files in /usr/sbin called

/usr/sbin/ppp-on
/usr/sbin/ppp-off

These contain templates for establishing a PPP session using chat. The ppp-off file was the one we used in the previous section to shut down our manually established PPP session (so we know it worked - if it did!).

Just in case you do not have these templates, here are modified and commented ones that we will be using:-


/usr/sbin/ppp-on-----------------
#!/bin/sh
#
#       ppp-on
#
#       Set up a PPP link
#
LOCKDIR=/var/lock   # Change this to suit your set up
DEVICE=cua1         # Change this to reflect the com port you are  using
PHONE=XXXXXXXX      # Change this to your ISP's phone number
USER=XXXX           # Your user name on the ISP's machine
PASSWORD=password
OUR_IP_ADDR=x.x.x.x # Your IP address for STATIC IP numbers, else 0.0.0.0

if [ -f $LOCKDIR/LCK..$DEVICE ]
then
   echo "PPP device is locked"
   exit 1
fi

fix-cua $DEVICE
(
   stty 19200 -tostop
# Change this to 115200 if your modem serial port can handle it!

   if chat -v -l LCK..$DEVICE ABORT "NO CARRIER" ABORT BUSY "" ATZ OK\
      ATDT$PHONE CONNECT "" ogin: $USER ssword: \\q$PASSWORD
   then
       pppd -d -detach asyncmap 0 modem crtscts $OUR_IP_ADDR: /dev/$DEVICE
       rm -f LCK..$DEVICE
       exit 0
   else
       echo "PPP call failed" 1>&2
       exit 1
   fi
) < /dev/$DEVICE > /dev/$DEVICE
#------END

/usr/sbin/ppp-off------------------


#!/bin/sh
DEVICE=ppp0
#
# If the ppp0 pid file is present then the program is running. Stop  it.
if [ -r /var/run/$DEVICE.pid ]; then
       kill -INT `cat /var/run/$DEVICE.pid`
#
# If unsuccessful, ensure that the pid file is removed.
#
       if [ ! "$?" = "0" ]; then
               echo "removing stale $DEVICE pid file."
               rm -f /var/run/$DEVICE.pid
               exit 1
       fi
#
# Success. Terminate with proper status.
#
       echo "$DEVICE link terminated"
       exit 0
fi
#
# The link is not active
#
echo "$DEVICE link is not active"
exit 1
#--------END

The file permissions and ownerships should be :-

-rwxr-xr-x   1 root     PPP          502 Sep  3 20:04 /usr/sbin/ppp-off
-rwxr-xr-x   1 root     PPP          612 Sep  3 20:04 /usr/sbin/ppp-on

14.1 What a Chat script means

The chat command bears some discussion. A chat script is a sequence of "expect string" "send string" pairs. In particular, note that we always expect something before we send something. If we are to send something WITHOUT receiving anything first, we must use an empty expect string (indicated by "") and similarly for expecting something without sending anything. Also, if a string consists of several words, (eg NO CARRIER), you must quote the string so that it is seen as a single entity (eg "NO CARRIER")

The chat line in our template is:-

if chat -v -l LCK..$DEVICE ABORT "NO CARRIER" ABORT BUSY "" ATZ OK\
   ATDT$PHONE CONNECT "" ogin: $USER ssword: \\q$PASSWORD

Basically, this says...

This is a very simple chat script. chat has considerable error recovery capability and a number of other option. For more information consult the chat manual page (man 8 chat).


Previous Next Table of Contents