1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| #!/bin/bash
# display usage function, no arg
function displayUsage
{
echo -e "Usage: $(basename $0) {\E[1m-h\E[0m <host1[,host2[,...]]>|\E[1m-H\E[0m <host_file>} \
{\E[1m-c\E[0m \"command list\"|\E[1m-C\E[0m <command_file>}\n\t\
host_file and command_file must be visible from host that run $(basename $0)."
}
# get script options
unset hstList
unset cmdList
while getopts ":h:H:c:C:" option
do
case $option in
h )
hstList=$(echo $OPTARG | tr ',' ' ')
;;
H )
hstList=$(cat $OPTARG | tr ',' ' ')
;;
c )
cmdList=$OPTARG
;;
C )
cmdList=$(cat $OPTARG)
;;
\? )
displayUsage
exit 1
;;
esac
done
if [[ -z $hstList ]] || [[ -z $cmdList ]]
then
displayUsage
exit 1
fi
# init color codes
initColor='\E[0;0;0m'
boldColor='\E[1m'
revColor='\E[1;37;40m'
redColor='\E[0;31;0m'
revRedColor='\E[1;31;40m'
# run command list on host list
for hst in $hstList
do
echo -e "$revColor# $hst$initColor"
output=$(ssh root@$hst $cmdList 2>&1)
retCode=$?
if [[ $retCode == 0 ]]
then
echo "$output"
elif [[ $retCode == 255 ]]
then
echo -e "$revRedColor### ssh error: $output$initColor"
else
echo -e "$boldColor$output$initColor"
fi
done |
Partager