Operating System - HP-UX
1831185 Members
2951 Online
110021 Solutions
New Discussion

Shellscripting remoteexecution

 
SOLVED
Go to solution
Lothar Krueler
Regular Advisor

Shellscripting remoteexecution

In a shellscript at a localhost I need the exitcode from a remote-executed script (remsh remotehost ... ). STDIO and STDERR are used for several messages otherwise. The remsh returns its own exitcode but not that from the executed command.
Can anyone tell me how to get the remote commands exit code for use on the localhost ?
Thanks in advance,
Lothar
Wissen macht zaghaft, Dummheit kann alles!
8 REPLIES 8
linuxfan
Honored Contributor

Re: Shellscripting remoteexecution

Hi Lothar,

You could do something like

remsh remotehost -l remote id -n "ls ; echo \$?"

This would return the output of ls and return 0 if succeeded and 1 if failed

-HTH
Ramesh
They think they know but don't. At least I know I don't know - Socrates
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Shellscripting remoteexecution

Hi Lothar:

The remsh exit code of 0 only means that the script was able to be launched. The exit status of the remote command is not returned. There is a workaround I have used for a long time.

REM_ERR_FILE=/var/tmp/x${$}.err
remsh remote_host my_command $REM_ERR_FILE
LOCAL_STAT=$?

$LOCAL_STAT only tells us about the success of remsh itself

Now my_coomand on the remote host can get the
value of $REM_ERR_FILE; any status values you are interested in can be written to that file by your script on the remote host.

When the remsh command finishes you can then
REM_STAT=`remsh remote_host cat ${REM_ERR_FILE}`
to capture the remote commands status. (You should then issue a final remsh to remove the $REM_ERR_FILE.)

It's a little complicated but it does work. Since we generate a process id dependent file fane on the local host, you don't have to worry about filename collision when multiple instances are running. This method also leaves stdderr and stdout for their normal use.


Hopes this gets you started, Clay

If it ain't broke, I can fix that.
Klaus Crusius
Trusted Contributor

Re: Shellscripting remoteexecution


A small shell function:

function remcall
{
host=$1; shift
remsh $host "$*; echo \"EXITCODE \$?\"" |
awk ' $1 == "EXITCODE" && NF == 2 { exit $2 }
1'
return $?
}

would return with the same exit code as the remote command. (Of course there must be no output on STDOUT which interfers with the EXITCODE output line).

Klaus

There is a live before death!
Lothar Krueler
Regular Advisor

Re: Shellscripting remoteexecution

Thanks!
Clay, im very glad about your fast and great asnwers ever and ever!
I will tell the remote command a remotefile for writing its exitcode. Then in an other remotecommand I'll read stout of something like "cat remotefile"

Lothar ;-))
Wissen macht zaghaft, Dummheit kann alles!
linuxfan
Honored Contributor

Re: Shellscripting remoteexecution

Hi,

If i had
remsh remotehost -l remoteid -n command; echo $?

then it would return the exit code of whether the remsh executed or not but my command was
remsh remotehost -l remoteid -n "command ; echo \$?"

so for example if i do this as a regular user (this is supposed to fail)
remsh remotehost -l remoteid -n "strings /etc/lvmtab ; echo \$?"

it comes back with
/etc/lvmtab: Permission denied
1
(Now this is the expected output )

I believe the key is to use the "-n" option and "command ; echo \$?" which gives the exit code of the command rather then the exit code of the remsh.

Lothar, to find out what -n does check the man page for remsh.

-Ramesh
They think they know but don't. At least I know I don't know - Socrates
Lothar Krueler
Regular Advisor

Re: Shellscripting remoteexecution

Hi Ramesh,
thanks,but the point is, that I can not use STDOUT and STDERR for getting the exitcode in this command because they are used otherwise even on the local host.
In this case it would be the easiest to use several remotecommands.
Lothar :-)
Wissen macht zaghaft, Dummheit kann alles!
James R. Ferguson
Acclaimed Contributor

Re: Shellscripting remoteexecution

Hi Lothar:

I agree that Clay's approach is perhaps the soundest. *However*, Ramesh's method (with the escaped return value) *does* work *IF*, as
Klaus points out, there is no other output.

Try this test:

# rexec servername -n "grep me /tmp/nofile;echo \$?"

The 'grep' returns <2> for a "nofile" condition but the return code is intermixed with "grep: can't open".

Regards!

...JRF...
linuxfan
Honored Contributor

Re: Shellscripting remoteexecution


Thanks guys,

i missed the point that you could not use the STDIO or STDERR. in which case the option is to redirect the exitcode to a file like Clay pointed out.

Once again thanks Clay/James/Lothar.

-Ramesh
They think they know but don't. At least I know I don't know - Socrates