Shell script implements whether the file is consistent across the line server _linux shell

Source: Internet
Author: User
Tags md5 ssh

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.