Fix bugs and improve output UI
- Fix hosts file existence check (was always false) - Fix loop to use while+read to handle spaces in hostnames - Filter empty lines from hosts file - Fix error exits to use exit 1 - Show progress counter [n/total] per host - Compact single-line output with colors - Show [DRY RUN] prefix when -d is set - Show per-host exit status (✓ ok / ✗ failed) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
f3c83317eb
commit
36c648d599
1 file changed
+33
-19
@@ -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
|
||||
Reference in new issue
Block a user