Saturday, January 8, 2022

[SOLVED] Verify command successfully executes over ssh in BASH

Issue

How does one verify that linux command is successfully executed over the ssh server? See the following example

ssh -qX 10.10.1.123 chmod -R 755 someDir/

I know that using the return code (0 or 1) method works but there are instances where the above command executes but the permissions do NOT get changed. Is there a better way to verify whether it executed successfully and permissions are changed accordingly?


Solution

There's no generic answer, because there's no generic problem. ssh does not return success to the caller unless the remote child gave a successful result.

For the specific problem, assuming your remote machine has a version of chmod that can incorrectly return unwarranted successful exit statuses, one might use:

if [[ $(ssh 10.10.1.123 'find someDir/ ! -perm 755 -print -quit') ]]; then
  echo "At least one file under someDir did not have its permissions changed to 755" >&2
fi


Answered By - Charles Duffy