Strange while loop issue

From: Dave Markham (dave.markham@fjserv.net)
Date: Tue Mar 20 2007 - 12:18:16 EST


People some of you have helped me with this script before, but im
finding some strange thing happening.

Here is a config file :-

# cat config
## comment lines
# Config file for blah blah
# machine:function
test1:openview
hosta:openview
hostb:openview
test2:openview
#test3:openview
#test4:openview

I want the script to get rid of comments etc and connect to a machine.
If it can connect what seems to happen is the while loop then breaks and
it doesnt get to the next machine. If the connection fails and times out
( as expected to test hosts ) then it does move to the next in the while
loop. I just cant figure it out, but i think something is happening
during the ssh call.

To note hosta and hostb are hosts which can be contacted and i have just
changed names for security. Same as the identity key for ssh. The test
hosts are made up and so will not work. Therefore hosta hostb should
echo "everything ok" and the tests should not.

Here is script and output when it is ran.

Script -----

#!/bin/ksh
set -x

con_test()
{
        con_server=$1
        con_test_res_list=$( ssh -i /dir/.ssh/something_id_rsa -o
ConnectTimeout=15 -o BatchMode=yes -o StrictHostKeyChecking=no -o
LogLevel=quiet root@${con_server} "uname -a" )

        # Now return file list to function call
        echo ${con_test_res_list}

}

config_set=$2
config_file="$1"

if [ "$config_set" ];then
        #IFS=": " #This is : space tab
        sed -e 's/#.*//' $config_file | egrep -v '^[ ]*$' | \
        while IFS=":" read REM_HOST REM_FUNC IGNORE
        do
                if [ ! "$REM_HOST" ] || [ ! "REM_FUNC" ];then
                        echo "\n\tERROR: Parsing config file
[$config_file] Check formatting. Exiting\n"
                        exit 2
                fi
                echo "host is [$REM_HOST], function is [$REM_FUNC]" #debug

                con_test ${REM_HOST} | read REM_HOST_CON_LIST

                # Result checked via variable
                if [ "${REM_HOST_CON_LIST}" ];then
                        echo "\n[Connection Test for
${REM_HOST}]\n\tSuccessful]\n\tFile list to backup [ ${REM_HOST_CON_LIST}]"
                else
                        echo "\n[Connection Test for
${REM_HOST}]\n\tFailed Connection"
                        # Skip to next server in while loop/config file
                        continue
                fi

                echo "Everything ok so now do extra commands and skip to
next in loop"
                # Here would be extra commands
        done
fi

Output of run -------

root@hub# ksh test3.ksh config y
+ config_set=y
+ config_file=config
+ [ y ]
+ read REM_HOST REM_FUNC IGNORE
+ IFS=:
+ sed -e s/#.*// config
+ egrep -v ^[ ]*$
+ [ ! test1 ]
+ [ ! REM_FUNC ]
+ echo host is [test1], function is [openview]
host is [test1], function is [openview]
+ read REM_HOST_CON_LIST
+ con_test test1
+ [ ]
+ echo \n[Connection Test for test1]\n\tFailed Connection

[Connection Test for test1]
        Failed Connection
+ continue
+ read REM_HOST REM_FUNC IGNORE
+ IFS=:
+ [ ! hosta ]
+ [ ! REM_FUNC ]
+ echo host is [hosta], function is [openview]
host is [hosta], function is [openview]
+ read REM_HOST_CON_LIST
+ con_test hosta
+ [ SunOS hosta 5.8 Generic_xxx-xxx sun4u sparc SUNW, ***** ]
+ echo \n[Connection Test for hosta]\n\tSuccessful]\n\tFile list to
backup [ SunOS hosta 5.8 _xxx-xxx sun4u sparc SUNW, *****]

[Connection Test for hosta]
        Successful]
        File list to backup [ SunOS hosta 5.8 _xxx-xxx sun4u sparc SUNW,
*****]
+ echo Everything ok so now do extra commands and skip to next in loop
Everything ok so now do extra commands and skip to next in loop
+ read REM_HOST REM_FUNC IGNORE
+ IFS=:
root@hub#
_______________________________________________
sunmanagers mailing list
sunmanagers@sunmanagers.org
http://www.sunmanagers.org/mailman/listinfo/sunmanagers



This archive was generated by hypermail 2.1.7 : Wed Apr 09 2008 - 23:41:47 EDT