Shell指令碼實現線上伺服器之間對比檔案是否一致_linux shell

來源:互聯網
上載者:User

工作中有時需要對比檔案是否一致,通過以下指令碼可以實現(比較簡單):分享出來,希望能協助到有同樣需求的朋友~

複製代碼 代碼如下:

#!/bin/bash
#對比伺服器上檔案是否一樣
# Richard shen 2012/07/08
LC_ALL='en_US.UTF-8'
basedir=`dirname $0`
HOST=$basedir/host.txt
PASSWD="abcd"   #密碼
FILE=$basedir/file.txt  
LOG=$basedir/tmp.log
>$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

對比的IP地址寫入host.txt,需要對比的檔案(支援多個檔案)寫入file.txt
輸出結果為:
複製代碼 代碼如下:

192.168.113.108 (./file.txt) MD5 compared files...
c84509bb3b109506935dba56b667a136  /data/server/www/apps/ad/fullcollumn.html
192.168.113.111 (./file.txt) MD5 compared files...
c84509bb3b109506935dba56b667a136  /data/server/www/apps/ad/fullcollumn.html
----------------------------------------------------------
192.168.113.108 (./file.txt) MD5 compared files...
d2c965f5222ff47432313c76863f428d  /data/server/www/apps/ad/sky.html
192.168.113.111 (./file.txt) MD5 compared files...
d2c965f5222ff47432313c76863f428d  /data/server/www/apps/ad/sky.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.