grab output from spawned command

From: Dave Markham (dave.markham@fjserv.net)
Date: Fri Feb 02 2007 - 10:19:54 EST


ok this one should hopefully be a little more tricky than the last few
posts for people.

Im trying to do a connection test to a machine before i start to do
other work on it, so basically i can bum out and write into a log file
if the machine cant be reached.

Way im thinking of doing it is by getting the pid of a spawned ssh
command and then sleeping for say 20 seconds and killing the pid if its
still there and echo some problem to a log. The below script doesnt show
that the bit calling the function is actually in a loop and so i dont
want the whole script to fail. The script will be calling multiple
machines see which is why i wan the pid for each time the ssh is down
for each separate machine so i have control.

Now using option 1 ssh command below works if you can get to the machine
or not
Option 2 only gets me $con_pid as the pid if the machine doesnt work,
otherwise it gives me the output of uname -n
I was trying Option 3 to put the output into a variable so i still get
con_pid as the spawned pid.

Now option 1 will work, but ideally i want to be able to get the spawned
pid so i can check if its there and kill if theres a problem later on,
AND get the output from uname -n ( im actually going to check for
existence of a remote file so im using this connection test for 2 things ).

Anyone know how i can do it?

I also tried doing

var=$( ${SSH} -l user $con_server "uname -n" 2>/dev/null & )

so i could capture the output but then i cant get the spawned pid number :(

#!/bin/ksh
#set -x
SSH=`which ssh`

# [Connection test]
con_test()
{
        # There wont be any yes/no question to ssh on connection
        # to a new server as we should have placed
        # StrictHostKeyChecking no
        # in ~/.ssh/config for the user running this script

        #
        # Want to get pid of spawned shell so can kill if
        # cannot connect to server
        con_server=$1
        out=""
        ${SSH} -l novab $con_server "uname -n" >/dev/null 2>&1 &
        # 1
        #${SSH} -l novab $con_server "uname -n" 2>/dev/null &
            # 2
        #${SSH} -l user $con_server "uname -n" 1>$out >/dev/null 2>&1
& # 3
        con_com=$? #Seems useless on spawned command
        con_pid=$!

        sleep 5

        # Return useful so function call can have an exit code
        # which can be called with $?. Can return 0-255
        #
        #return something

        #Echo variables so they are caught in calling read
        echo "$con_pid $out"
}

REM_HOST=$1

echo "Checking connection to remote host [$REM_HOST]"

con_test $REM_HOST |read rem_host_con_pid con_ssh_out

echo "ssh connect pid is [$rem_host_con_pid]"
echo "output from ssh if success is [$con_ssh_out]"
_______________________________________________
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:35 EDT