Operating System - HP-UX
1833882 Members
2025 Online
110063 Solutions
New Discussion

Passing exit code vis SSH

 
Shahul
Esteemed Contributor

Passing exit code vis SSH

Hi,

All I want is to check the version of a software on a remote system through SSH. Thought of greping the version and then passing exit code, but it doesn't seems to be working. Any idea? or any better way of doing this. For eg.

ssh "/usr/sbin/swlist xxx |grep 1234".

TIA
Shahul
6 REPLIES 6
Steven E. Protter
Exalted Contributor

Re: Passing exit code vis SSH

Shalom,

1) Try grep -i you may be failing a case sensitive issue.

2) Return codes are passed by exit

exit 1 bad
exit 0 good.

Call a script on the remote node and have it with exit code based on success or failure.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Shahul
Esteemed Contributor

Re: Passing exit code vis SSH

Hi Steve,

I think you missed my question.

ssh "/usr/sbin/swlist xxx |grep 1234;echo $?"

gives always "0", don't know why, it's same for remsh.

#ssh "/usr/sbin/swlist xxx |grep 1234"
#echo $?

This will also give always "0", because the SSH is always successful. I am looking for the exit code of "swlist | grep".

Hope I made it clear.

Regards
Shahul
Marvin Strong
Honored Contributor

Re: Passing exit code vis SSH

Its always zero because you are testing the exit code of your ssh command not the exit code of the command on the remote system.

Marvin Strong
Honored Contributor

Re: Passing exit code vis SSH

if you want to get the return code you have to do what steven said run a script and exit with the return code.

ssh server /tmp/test

test script:

#!/usr/bin/ksh

swlist | grep 1234
exit $?

then if you echo $? you should get the proper error code.
Marvin Strong
Honored Contributor

Re: Passing exit code vis SSH

*exit code not error code.

guess im doing too many things at once today.
A. Clay Stephenson
Acclaimed Contributor

Re: Passing exit code vis SSH

Here is what you should do:


XX=$(ssh "/usr/sbin/swlist xxx |grep -q 1234; echo ${?}".
echo "XX = ${XX}" # will be 0 if 1234 was matched; 1 otherwise"

an alternate approach:
ssh "/usr/sbin/swlist xxx |grep -q 1234; echo ${?}" | read XX
echo "XX = ${XX}" # will be 0 if 1234 was matched; 1 otherwise"

Note that grep -q does its work silently and outputs nothing; we are only interested in a zero or non-zero exit status.


If you test ${?} on the local host, all you are doing is testing whether or not ssh was able to be spawned.


If it ain't broke, I can fix that.