At work, you sometimes need to compare whether the files are consistent. You can use the following script (relatively simple): share it and hope to help friends who have the same requirements ~
The code is as follows: |
Copy code |
#! /Bin/bash # Compare to whether files on the server are the same # Richard shen 2012/07/08 LC_ALL = 'en _ US.UTF-8' Basedir = 'dirname $0' HOST = $ basedir/host.txt PASSWD = "abcd" # Password FILE = $ basedir/file.txt LOG = $ basedir/tmp. log > $ LOG [! -F/usr/bin/nc] & yum-y install nc [! -F/usr/bin/keep CT] & yum-y install keep CT Auto_smart_ssh (){ CT-c "set timeout-1; Spawn ssh-o StrictHostKeyChecking = no $2 $ {@: 3 }; CT { * Assword: * {send -- $ 1r; CT { * 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 [32 m $ host ($ FILE) MD5 compared files... e [0 m" Auto_smart_ssh $ PASSWD root @ $ host md5sum $ file | grep-v StrictHostKeyChecking | tee-a $ LOG Fi Done Echo "----------------------------------------------------------" Done |
Compare the IP address to the host.txt file, and compare the file (support multiple file types to the File.txt file)
Output result:
The code is as follows: |
Copy code |
192.168.113.108 (./file.txt) MD5 compared files... C84509bb3b450506935dba56b667a136/data/server/www/apps/ad/fullcollumn.html 192.168.113.111 (./file.txt) MD5 compared files... C84509bb3b450506935dba56b667a136/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 |