#! /usr/bin/expect -f
#*********************************************************************
#   Copyright (c) International Business Machines  Corp., 2000
#
#   This program is free software;  you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY;  without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
#   the GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this pronram;  if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#
#
#  FILE   : telnet
#
#  PURPOSE: Tests the basic functionality of `telnet`. 
#
#  SETUP: The program `/usr/bin/expect' MUST be installed.
#	  The PASSWD and RHOST variables MUST be set prior to execution. 
#
#  HISTORY:
#    03/01 Robbie Williamson (robbiew@us.ibm.com)
#      -Ported
#
#*********************************************************************
# 
# telnet perform a telnet session to each host in HOST_LIST with user 
#    RUSER for a count of LOOPCOUNT and does an ls -l /etc/hosts to
#    verify that the telnet was established.
#
#*********************************************************************

set TC telnet
set TCtmp "/tmp"
set SLEEPTIME 3
set TESTLOG "$TCtmp"

if [info exists env(RUSER)] {
   set RUSER $env(RUSER)
} else {
   set RUSER root
} 

if [info exists env(PASSWD)] {
   set PASSWD $env(PASSWD)
} else {
   set PASSWD .pasroot
   send_user "PASSWD NOT SET:Using .pasroot as the PASSWD variable for $RUSER. \n"
} 

if [info exists env(RHOST)] {
   set RHOST $env(RHOST)
} else {
   send_user "Please set/export the RHOST variable. \n"
   exit 1
} 

set timeout 90

if [info exists env(LOOPCOUNT)] {
   set LOOPCOUNT $env(LOOPCOUNT)
} else {
   set LOOPCOUNT 25
}

# stty echo
send_user " Starting\n"

# Do foreach host on command line
set count 0
while {$count < $LOOPCOUNT} {
   set count [expr $count+1]
   foreach HOST $RHOST { 
      send_user "Host: $HOST\n"

      # telnet to the host
      spawn telnet $HOST
      expect -re "login:"
      sleep 1
      send "$RUSER\r"
      expect -re "Password:"
      sleep 1
      send "$PASSWD\r"

      # Wait for shell prompt
      expect -re "#"

      # Run passwd command - and respond to its prompts
      send "ls -l /etc/hosts | wc -w > $TESTLOG/$RUSER.$HOST \r" 
      # When shell prompt comes back, logout

      expect -re "#"
      exp_send "logout\r"

      send_user "CHECKING TELNET STATUS\n"
      set nummatch [exec rsh -n -l $RUSER $HOST "cat $TESTLOG/$RUSER.$HOST|grep -c 9"] 
      if {$nummatch==1} {
         send_user "$TC interactive Test Successful in LOOP $count\r"
         exec rsh -n -l $RUSER $HOST "rm -f $TESTLOG/$RUSER.$HOST" 
      } else {
         send_user "$TC interactive session failed\n"
         exit 1
      }
   }
}

send_user "\nTest PASSES\n\n"
exit 0
