Re: ssh command stderr, stdout and exit code (531 Views)
Reply
Occasional Contributor
jbagley2
Posts: 11
Registered: ‎07-09-2013
Message 1 of 2 (561 Views)

ssh command stderr, stdout and exit code

Hi!

 

I'm trying to capture standard error, standard out and the exit code of a unix program in an SSH Command flow step.  Here's my unix script for testing:

 

#!/bin/sh

echo "standard out"
echo "standard error" >&2

exit 7

 The script executes fine using the SSH Command step, but the results are munged.  Here's the raw result:

 

{returnCode=-1;STDERR=;returnResult=;Result=;sessionId=iconclude4728939292462036583;STDOUT=standard out
standard error
;}

 You can see that the Result and returnResult are blank and the standard error was combined with standard out.  The error code (7) is nowhere to be seen.  I have a unix process that returns error codes from 0 - 255 and different action needs to be taken based on the error code combined with standard error.

 

This is with OO version 9.0.7

 

Please use plain text.
Occasional Contributor
jbagley2
Posts: 11
Registered: ‎07-09-2013
Message 2 of 2 (531 Views)

Re: ssh command stderr, stdout and exit code

 

Ok, I solved my own problem by writing a wrapper script that returns easily parseable XML.  There are probably a thousand ways to do this and mine is not the most elegant, robust or complex.  But, it works for me.

 

Filename: oowrap.sh

#!/bin/bash

##
# Wrapper script for running unix commands from HPOO Flow.
#  - Returns easily parseable XML.
#  - Separates STDOUT and STDERR.
#  - Captures process id numbers.
#  - Captures exit code.
#
# Useage:
#  oowrap.sh /path/to/command -any -argument -you -want


progname=`echo $1 | awk -F\/ '{ print $NF }'`
stderr="/tmp/$progname.$$.stderror"
stdout="/tmp/$progname.$$.stdout"

$* 2>$stderr 1>$stdout &
cmdpid=$!
wait $cmdpid
exitval=$?

cat - << XML
<exec bashversion="$BASH_VERSION">
  <parentpid>$PPID</parentpid>
  <wrapperpid>$BASHPID</wrapperpid>
  <uid>$EUID</uid>
  <gid>$GROUPS</gid>
  <hostname>$HOSTNAME</hostname>
  <programpid>$cmdpid</programpid>
  <programname>$progname</programname>
  <exitvalue>$exitval</exitvalue>
  <stdout>
XML
cat $stdout
cat - << XML
  </stdout>
  <stderror>
XML
cat $stderr
cat - << XML
  </stderror>
</exec>
XML

rm $stderr
rm $stdout

if [ $exitval -ne 0 ]
then
        exit 1
fi

 

Using this is simple enough, just apply the "XML Get Element Value" result filter to the stdout result.

 

exitvalue.jpg

 

It seems to me like it would be easy to include this functionality in the "SSH Command" flow step, would be a lot cleaner than what I'm having to do here.

 

Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation