#! /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   : rlogin
#
#  PURPOSE: Tests the basic functionality of `rlogin`. 
#
#  SETUP: The program `/usr/bin/expect' MUST be installed.
#         The home directory of root on the machine exported as "RHOST"
#         should have a ".rhosts" file with the hostname of the machine
#         where the test is executed, OR the "PASSWD" section below MUST
#	  be uncommented and set. 
#
#  HISTORY:
#    03/01 Robbie Williamson (robbiew@us.ibm.com)
#      -Ported
#
# 
# rlogin perform a rlogin 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 rlogin was established.
#
#*********************************************************************

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

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

set RHOST $env(RHOST)
set timeout 10

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

# stty echo
send_user " Starting\n"

set count 0
while {$count < $LOOPCOUNT} {
   set count [expr $count+1]
      send_user "Host: $RHOST\n"

      # rlogin to the host
      spawn rlogin $RHOST -l $RUSER
      
      # Uncomment code below and add root's passwd if .rhosts file is not 
      # present on remote host.
      #---------------------------------
      #set PASSWD "<ROOT PASSWORD HERE>"
      #expect -re "Password:"
      #send "$PASSWD\r"

      # Wait for shell prompt
      expect -re "$RUSER@"

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

      expect -re "$RUSER@"
      exp_send "logout\r"

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

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