Sometimes you need to compare files consistently in your work, and you can do it with the following script (simpler): share it, hoping to help friends with the same needs
Copy Code code as follows:
#!/bin/bash
#对比服务器上文件是否一样
# Richard Shen 2012/07/08
Lc_all= ' en_US. UTF-8 '
Basedir= ' DirName $ '
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 $ ${@:3};
Expect {
*assword:* {send-$1\r;
Expect {
*denied* {exit 2;}
Eof
}
}
EOF {exit 1;}
}
"
# return $?
}
Num=0
For file in ' Cat $FILE ';d o
For host in ' Cat $HOST ';d o
[[$host =~ "^#"]] && continue
Let ' num++ '
if! /usr/bin/nc-w 1 $host >/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
Compared to the IP address written to host.txt, need to compare files (support multiple files) to write file.txt
The output results are:
Copy Code code as follows:
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