Operating System - HP-UX
1753706 Members
4877 Online
108799 Solutions
New Discussion юеВ

error handling remsh/ssh with script's

 
SOLVED
Go to solution
Billa-User
Regular Advisor

error handling remsh/ssh with script's

hello,

how it is the best way to handle error codes with command "remsh/ssh" ?

example :
- i have 2 scripts "main.sh", "sub.sh"
- "main.sh" starts with remsh "sub.sh"
- i want handle the errors of "sub.sh" and also i want to get output's of "sub.sh"

main.sh
remsh server "/tmp/sub.sh /not-found"

sub.sh
if [ ! -d $1 ]
then
echo "dir: $1 not found";exit 1
fi
exit 0

i tried several versions, i think this is the best ?
# tee and parse a temporary logfile
remsh server -n '( /tmp/sub.sh /not-found; echo "RETURN: $?" )' 2>&1 | tee /tmp/error_log.log
# return code in last character
rc=$( sed -n '$s/.*\(.\)$/\1/p' /tmp/error_log.log )

when i use
rc=`remsh server -n '(/tmp/sub.sh /not-found; echo $?)' `
i can't get the output with "\n" , and a variable has also a limit of 512 bytes ?

regards
4 REPLIES 4
James R. Ferguson
Acclaimed Contributor
Solution

Re: error handling remsh/ssh with script's

Hi:

For simple cases (and here I'm going to use 'ssh' in lieu of 'remsh'), you can do:

# ssh -n server /home/billa/sub.sh
# RC=$?
# [ ${RC} = 0 ] echo "ok" || echo "bad"

If you want to capture output from the remotely run process you can do so into a file on either the local or the remote host.

# ssh -n server /home/billa/sub.sh > /home/billa/local.log

...or if you quote:

# ssh -n server /home/billa/sub.sh ">" /var/tmp/billa_remote.log

In either case you have the ability to capture the return code as first shown.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: error handling remsh/ssh with script's

It looks like you know that remsh(1) doesn't capture the exit status and you need to do that echo. And ssh(1) works fine.

>I can't get the output with "\n"

You should be able to find the last line with the exit status:
echo "$rc" | tail -1

>a variable has also a limit of 512 bytes?

No, you can have very long strings.
Billa-User
Regular Advisor

Re: error handling remsh/ssh with script's

>a variable has also a limit of 512 bytes?
can you please tell me, what is the limit of a shell variable ?

the problem of remsh/ssh is to capture the two
errors of remsh/ssh command:

first : error of remsh
second : error of remote command

a also to get an output of the error.

regards
Dennis Handly
Acclaimed Contributor

Re: error handling remsh/ssh with script's

>scan you please tell me, what is the limit of a shell variable?

Limited to heap space, I was able to get about 400 Mb:
sizeof(var): 209715200
./itrc_big_var.sh[48]: no space

>second: error of remote command

You did this by that "echo $?" on the remote side.