how to force rsh to exit with status of remote command

how to force rsh to exit with status of remote command

Post by R. P. C. Rodgers, M. » Tue, 04 Jun 1991 14:50:05



Dear Fellow Netlanders,

Reading the manual pages concerned with rsh and experimentation thus
far fail to show how rsh can be made to exit with the status of the command
it is executing on a remote host (if indeed this is possible...).
The rsh always seems to exit with a status of 0, even if the command on the
remote hosts fails.

Thanks in advance for any pointers/solutions.

Cheerio, Rick Rodgers
R. P. C. Rodgers, M.D.         (415)476-2957 (work) 664-0560 (home)
UCSF Laurel Heights Campus     UUCP: ...ucbvax.berkeley.edu!cca.ucsf.edu!rodgers


 
 
 

how to force rsh to exit with status of remote command

Post by Tom Christians » Tue, 04 Jun 1991 22:27:44



:Reading the manual pages concerned with rsh and experimentation thus
:far fail to show how rsh can be made to exit with the status of the command
:it is executing on a remote host (if indeed this is possible...).
:The rsh always seems to exit with a status of 0, even if the command on the
:remote hosts fails.

rsh will exit !0 if it fails to make the connection, or loses it,
or whatnot.  I use maarten Litmaath's "ersh" front end [enclosed]
when I want an rsh that gives me the remote status.

--tom

#!/bin/sh

# This rsh front-end returns the exit status of the remote command.
# It works OK with sh/csh-compatible shells on the remote (!) side.
# If there is no remote command present, /usr/ucb/rlogin is invoked.
# Usage: see rsh(1).

unset hostname lflag nflag user

case $1 in
-l)
        ;;
*)
        hostname=$1
        shift
esac

case $1 in
-l)
        lflag=-l
        user=$2
        shift 2
esac

case $1 in
-n)
        nflag=-n
        shift
esac

case $hostname in
'')
        hostname=$1
        shift
esac

case $# in
0)
        exec /usr/ucb/rlogin $lflag ${user+"$user"} "$hostname"
esac

AWK='
        NR > 1 {
                print prev;
                prev = $0;
                prev1 = $1;
                prev2 = $2;
        }
        NR == 1 {
                prev = $0;
                prev1 = $1;
                prev2 = $2;
        }
        END {
                if (prev1 ~ /[0-9]*[0-9]0/)
                        exit(prev1 / 10);
                if (prev1 == "0")
                        exit(prev2);
                print prev;
                exit(1);
        }
'

exec 3>&1

/usr/ucb/rsh "$hostname" $lflag ${user+"$user"} $nflag \
        "(${*-:}); sh -c '"'echo "$0 $1" >&2'\'' $?0 "$status"' \
        2>&1 >&3 3>&- | awk "$AWK" >&2 3>&-
--

            "Perl is to sed as C is to assembly language."  -me

 
 
 

how to force rsh to exit with status of remote command

Post by Andy Behre » Wed, 05 Jun 1991 03:01:51



Quote:>The rsh always seems to exit with a status of 0, even if the command
> on the remote hosts fails.

This question gets asked fairly often.  Here's a replacement for rsh
that you should install on the local machine. The remote machine
doesn't need to be changed provided you are running a fairly "normal"
shell.

Thanks to Maarten Litmaath for this little gem.

------------------------------ CUT HERE ------------------------------
#!/bin/sh

# This rsh front-end returns the exit status of the remote command.
# It works OK with sh/csh-compatible shells on the remote (!) side.
# If there is no remote command present, /usr/ucb/rlogin is invoked.
# Usage: see rsh(1).

RSH=/usr/ucb/rsh
RLOGIN=/usr/ucb/rlogin

hostname=
lflag=
nflag=
user=

case $1 in
-l)
        ;;
*)
        hostname=$1
        shift
esac

case $1 in
-l)
        lflag=-l
        user=$2
        shift 2
esac

case $1 in
-n)
        nflag=-n
        shift
esac

case $hostname in
'')
        hostname=$1
        shift
esac

case $# in
0)
        exec $RLOGIN $lflag ${user+"$user"} "$hostname"
esac

AWK='
        NR > 1 {
                print prev;
                prev = $0;
                prev1 = $1;
                prev2 = $2;
        }
        NR == 1 {
                prev = $0;
                prev1 = $1;
                prev2 = $2;
        }
        END {
                if (prev1 ~ /[0-9]*[0-9]0/)
                        exit(prev1 / 10);
                if (prev1 == "0")
                        exit(prev2);
                print prev;
                exit(1);
        }
'

exec 3>&1

$RSH "$hostname" $lflag ${user+"$user"} $nflag \
        "(${*-:}); sh -c '"'echo "$0 $1" >&2'\'' $?0 "$status"' \
        2>&1 >&3 3>&- | awk "$AWK" >&2 3>&-
--------------------------- END OF SCRIPT ---------------------------

 
 
 

how to force rsh to exit with status of remote command

Post by rodg.. » Thu, 06 Jun 1991 02:38:55



Quote:>Thanks to Maarten Litmaath for this little gem.

The Litmaath script is indeed a gem; we have modified it a bit so that it
can, upon specification of an option, save the status in a file, which is
useful if the rsh command is placed in the background from within a script.
One can then carry on with other work, then use the wait command to ensure
that rsh is done, read the exit status from the file, and act appropriately.

So on to a related problem: if one puts a LOCALLY executed command into the
background from within a script, then later issues a wait command so as to be
able to do something with output from the command, how can one cause the exit
status for the background command to have been saved in a file?

Cheerio, Rick Rodgers
R. P. C. Rodgers, M.D.         (415)476-2957 (work) 664-0560 (home)
UCSF Laurel Heights Campus     UUCP: ...ucbvax.berkeley.edu!cca.ucsf.edu!rodgers


 
 
 

how to force rsh to exit with status of remote command

Post by Maarten Litmaa » Fri, 07 Jun 1991 04:25:38




Quote:>[...] we have modified it a bit so that it
>can, upon specification of an option, save the status in a file, which is
>useful if the rsh command is placed in the background from within a script.
>One can then carry on with other work, then use the wait command to ensure
>that rsh is done, read the exit status from the file, and act appropriately.

You need not modify `ersh' to accomplish what you want.
This is how to do it in the Bourne shell:

        ersh machine cmd < input > output 2> errors &
        pid=$!          # remember the process ID
        # lots of other commands
        wait $pid       # wait for the specified process
        status=$?       # the exit status of the process is returned

If you _must_ use a file, use the following:

        (ersh machine cmd redirections; echo $? > status_file) &

In the csh you would have to resort to this:

        ((ersh machine cmd < input > output) >& errors; \
                echo $status > status_file) &

Quote:>So on to a related problem: if one puts a LOCALLY executed command into the
>background from within a script, then later issues a wait command so as to be
>able to do something with output from the command, how can one cause the exit
>status for the background command to have been saved in a file?

That question has been answered by now.
 
 
 

1. How to get the exit status of a remote command executed via rsh

Here's an easy one for all you unix experts:

I'm trying to get the exit status of a command executed thru rsh

It is part of a csh script and looks something like this

        rsh -n <hostname> <command>
        if($status) then
                echo "Error"
        endif

When <command> has an error condition, rsh still exits with an error status of
'0' because rsh was successful even though <command> wasnt.

How could I get around this?

I dont have access to usenet, so I'd appreciate direct replies to
my email address

Thanks in advance,
Sabitha.

--

2. x86 and ppp

3. ersh - rsh frontend hack returning exit status of remote cmd

4. 127.0.0.1: Network is unreachable !!!

5. Getting exit status for a command completed by rsh.

6. desktop menubar: how to get rid of

7. exit code of the remote command execution (done via rsh)

8. ATAPI CD-ROM Problems

9. Exit status of remote commands

10. Retrieving Exit Status of remote command

11. remote command's exit status

12. Finding the exit status of a command run on the remote shell

13. rsh - getting the status of an rsh'd command