diff --git a/csc b/csc index 972bd1a..ec16497 100755 --- a/csc +++ b/csc @@ -81,20 +81,20 @@ fi if [ -z "$group" ] then - echo "Error: group is not defined !" + echo "Error: group is not defined!" echo usage - exit + exit 1 fi hosts="$confdir/$group" -if [ -z "$hosts" ] +if [ ! -f "$hosts" ] then - echo "Error: hosts file $confdir/$group do not exists !" + echo "Error: hosts file $confdir/$group does not exist!" echo usage - exit + exit 1 fi # see http://askubuntu.com/questions/506158/unable-to-initialize-frontend-dialog-when-using-ssh @@ -105,24 +105,38 @@ FRONTEND="TERM=$TERM DEBIAN_FRONTEND=dialog" if [ "$verbose" = true ] then echo "> will do :" - echo " grep -v '^ *#' < \"$hosts\" | while IFS= read -r line; do ssh -n -t \"\$line\" \"$FRONTEND $cmd\"; done" + echo " grep -v '^ *#' \"$hosts\" | grep -v '^ *$' | while IFS= read -r line; do ssh -t \"\$line\" \"$FRONTEND $cmd\"; done" fi -if [ "$dryRun" = false ] -then - for line in $(grep -v '^#' "$hosts") - do - echo -e "\e[34m------------------------------------------------------------\e[0m" - echo -e "\e[34m> \e[32m\"$cmd\" \e[34mon \e[32m$line\e[0m" - echo -e "\e[34m> \e[33mssh -t \"$line\" \"$FRONTEND $cmd\"\e[0m" - echo -e "\e[34m------------------------------------------------------------\e[0m" - ssh -t "$line" "$FRONTEND $cmd" - done -fi +mapfile -t host_list < <(grep -v '^ *#' "$hosts" | grep -v '^ *$') +total=${#host_list[@]} + +for i in "${!host_list[@]}"; do + line="${host_list[$i]}" + n=$((i + 1)) + + if [ "$dryRun" = true ]; then + prefix="\e[33m[DRY RUN]\e[0m " + else + prefix="" + fi + + echo -e "${prefix}\e[34m[$n/$total]\e[0m \e[32m\"$cmd\"\e[0m \e[34mon\e[0m \e[32m$line\e[0m \e[34m→\e[0m \e[33mssh -t \"$line\" \"$FRONTEND $cmd\"\e[0m" + + if [ "$dryRun" = false ]; then + ssh -t "$line" "$FRONTEND $cmd" + rc=$? + if [ $rc -eq 0 ]; then + echo -e "\e[32m✓ ok\e[0m" + else + echo -e "\e[31m✗ failed (exit $rc)\e[0m" + fi + fi +done if [ "$verbose" = true ] then - echo "> All done, bye." + echo "> All done, bye." fi -exit +exit 0