Skip to content

Instantly share code, notes, and snippets.

@schlomo
Last active February 20, 2018 11:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schlomo/7908037 to your computer and use it in GitHub Desktop.
Save schlomo/7908037 to your computer and use it in GitHub Desktop.
rsh wrapper that correctly returns the exit code of the remote command, just like ssh does. This wrapper installs itself as rsh and calls the real rsh. Written for Bash.
# This rsh wrapper supports reporting the exit code of the remote program.
# Assumes that remote shell is Bash or compatible and that you don't call
# exit directly (that would yield a return 127 regardless of the remote
# exit code!)
#
# Written by Schlomo Schapiro @ ImmobilienScout24
# Licensed under the GNU General Public License, see http://www.gnu.org/licenses/gpl.html for full text
#
function rsh {
local res=$(
if [[ "$1" ]] ; then
command -p rsh "$@" \; echo -n 'RSH_REMOTE_EXIT_CODE=$?'
else
command -p rsh
fi
) || return $?
local exit_code="${res##*RSH_REMOTE_EXIT_CODE=}"
if [[ ! -n "$exit_code" ]]; then
exit_code=127
fi
res="${res%%RSH_REMOTE_EXIT_CODE*}"
echo -n "$res"
return $exit_code
}
rsh other.host sudo service foobar start || fail_test Could not start foobar service
rsh other.host sudo service foobar status || fail_test Service not running after start
rsh other.host sudo service foobar stop || fail_test Could not stop foobar service
! rsh other.host sudo service foobar status || fail_test Service still active after stop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment