#!/bin/sh
#
# ccm - Cisco Configuration Manager
#
# Usage:  ccm <cisco_file
#
# Shell script to grab the configurations from a set of Cisco routers and/or
# terminal servers, compare them with previously grabbed configurations that
# are maintained in SCCS, and save any configuration changes into SCCS.  If
# any changes are noted for a given configuration file, email is sent to a
# user-definable list detailing the differences (this is simply the "diff"
# output of the changes).  This script uses the "cnct" program to connect to
# the telnet port of the Ciscos and grab the configurations.  A simple awk
# script is used to strip off the non-configuration output, and to remove any
# Cisco comments (lines that start with a "!" character).  It then uses a
# few simple shell commands to detect the changes, and install them into SCCS.
# If an SCCS file for the router or terminal server does not yet exist, one
# is created for next time and appropriate email is generated.  Attempts are
# made to deal with any likely problems in a reasonable way.
#
# This script is most useful when run via "cron" (we currently run it three
# times a day).
#
#
# Written by:	Dana J. Dawson
#		Sr. Network Analyst
#		Cray Research, Inc.
#		(612) 683-3056
#		dana@cray.com
#		March, 1993
#
# Inspired by a utility developed by Jeff Wabik, Minnesota Supercomputer Center
#
#
# Set the following variables to the appropriate values.
# They should all be pretty obvious...

CISCODIR="/home/taurus2/dana/cisco/ccm"
CMDS="$CISCODIR/commands.ccm"
ADDR="dana,madman,snoot,gt350"
PROG="/home/taurus2/dana/bin/cnct"

cd $CISCODIR

while read CISCO PW1 EPW
do
  /usr/etc/ping $CISCO 2>/dev/null | egrep -s "alive"
  if (test $? = 0)
  then
    $PROG -p $PW1 -e $EPW -c $CMDS $CISCO \
      | awk -f ccm.awk \
      | tr -d '\015'     > $CISCO.new
    case $? in
    0)  if (test -s $CISCO.new)
        then
          tail -1 $CISCO.new | egrep -s '^end$'
          if (test $? = 0)
          then
            if (test -r SCCS/s.$CISCO -a -w ",$CISCO")
            then
              sccs edit $CISCO 1>/dev/null 2>&1
              diff -c4 $CISCO $CISCO.new >$CISCO.diffs
              case $? in
              0)  sccs unedit $CISCO 1>/dev/null 2>&1
                  ;;
              1)  mv $CISCO.new $CISCO 1>/dev/null 2>&1
                  echo "" 1>>$CISCO.diffs
                  echo "########## SCCS Summary ##########" 1>>$CISCO.diffs
                  sccs delta -y"" $CISCO 1>>$CISCO.diffs 2>/dev/null
                  mail -s "CCM: $CISCO Configuration changes" $ADDR <$CISCO.diffs
                  ;;
              *)  sccs unedit $CISCO 1>/dev/null 2>&1
                  mail -s "CCM: $CISCO:  'diff' problem - skipping" $ADDR </dev/null
                  ;;
              esac
              /bin/rm -f $CISCO.diffs 1>/dev/null 2>&1
            else
              mv $CISCO.new $CISCO 1>/dev/null 2>&1
              sccs create $CISCO 1> $CISCO.add 2>&1 
              mail -s "CCM: Added $CISCO to SCCS" $ADDR <$CISCO.add
              /bin/rm -f $CISCO.add 1>/dev/null 2>&1
            fi
            sccs clean 1>/dev/null 2>&1
          else
            mail -s "CCM: Incomplete $CISCO config" $ADDR </dev/null
          fi
        else
          mail -s "CCM: Empty $CISCO config - check password(s)" $ADDR </dev/null
        fi
        ;;
    1)  mail -s "CCM: $CISCO did not complete" $ADDR </dev/null
        ;;
    2)  mail -s "CCM: $CISCO timed out" $ADDR </dev/null
        ;;
    esac
    /bin/rm -f $CISCO.new 1>/dev/null 2>&1
  else
    mail -s "CCM: $CISCO appears to be down" $ADDR </dev/null
  fi
done
