SoFunction
Updated on 2025-03-10

Shell scripts to achieve whether the files are consistent between online servers


#!/bin/bash
#Compare whether the files on the server are the same
# Richard shen 2012/07/08
LC_ALL='en_US.UTF-8'
basedir=`dirname $0`
HOST=$basedir/
PASSWD="abcd"   #Password
FILE=$basedir/  
LOG=$basedir/
>$LOG
[ ! -f /usr/bin/nc ] && yum -y install nc
[ ! -f /usr/bin/expect ] && yum -y install expect
auto_smart_ssh () {
    expect -c "set timeout -1;
        spawn ssh -o StrictHostKeyChecking=no $2 ${@:3};
                 expect {
                         *assword:* {send -- $1\r;
                         expect {
                             *denied* {exit 2;}
                             eof
                                }
                         }
                  eof     {exit 1;}
                  }
                                  "
 #   return $?
 }
num=0
for file in `cat $FILE`;do
   for host in `cat $HOST`;do
       [[ $host =~ "^#" ]] && continue
       let 'num++'
       if ! /usr/bin/nc -w 1 $host 22 > /dev/null; then
          echo " ssh connect failed." | tee -a $LOG
          continue
       else
          echo -e "\e[32m$host ($FILE) MD5 compared files...\e[0m"
          auto_smart_ssh $PASSWD root@$host md5sum $file | grep $file | grep -v StrictHostKeyChecking | tee -a $LOG
      fi
    done
echo "----------------------------------------------------------"
done