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 ~ #! /Bin/bash # compare whether files on the server are the same # Richard shen2012/07/08 # BLOG: http://lxsym.blog.51cto.comLC _ ALL = 'en _ US.UTF-8 'basedir = 'dirname $0'
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 ~
#! /Bin/bash
# Compare to whether files on the server are the same
# Richard shen 2012/07/08
# BLOG: http://lxsym.blog.51cto.com
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 -- $1 \ r;
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:
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