shell实用脚本1

参考公众号民工哥技术之路

场景一:

        有两台服务器,a服务器的IP为11.0.1.18,b服务器的IP为11.0.1.12,都有个目录/app/tmp/test,我们需要比较这个目录里面的文件的一致性

#!/bin/bash
#####################################
#检测两台服务器指定目录下的文件一致性
#####################################
#通过对比两台服务器上文件的 md5 值, 达到检测一致性的目的
dir=/app/tmp/test
b_ip=11.0.1.12
#将指定目录下的文件全部遍历出来并作为md5sum命令的参数,进而得到所有文件的md5值,并写入到指定文件中
find $dir -type f|xargs md5sum > /tmp/md5_a.txt
echo "please input server_b password"
ssh $b_ip "find $dir -type f|xargs md5sum > /tmp/md5_b.txt"
echo "please input server_b password"
scp  $b_ip:/tmp/md5_b.txt /tmp
#将文件名作为遍历对象进行一一比对
for f in `awk '{print $2}' /tmp/md5_a.txt`;do
    #以a机器为标准,当b机器不存在遍历对象中的文件时直接输出不存在的结果
    if grep -qw "$f" /tmp/md5_b.txt;then
        md5_a=`grep -w "$f" /tmp/md5_a.txt|awk '{print $1}'`
        md5_b=`grep -w "$f" /tmp/md5_b.txt|awk '{print $1}'`
        #当文件存在时, 如果 md5 值不一致则输出文件改变的结果
        if [ $md5_a != $md5_b ];then
            echo "$f changed."
        fi
    else
        echo "$f deleted."
    fi
done

测试:

#11.0.1.18上测试目录
[root@ansible01 tmp]# tree /app/tmp/test/
/app/tmp/test/
├── 10.txt
├── 1.txt
├── 2.txt
├── 3.txt
├── 4.txt
├── 5.txt
├── 6.txt
├── 7.txt
├── 8.txt
├── 9.txt
└── wyx.txt
#11.0.1.12上测试目录
[root@k8s-master test]# tree /app/tmp/test/
/app/tmp/test/
├── 10.txt
├── 1.txt
├── 2.txt
├── 3.txt
├── 4.txt
├── 5.txt
├── 6.txt
├── 7.txt
├── 8.txt
└── 9.txt
#运行脚本
[root@ansible01 tmp]# sh check_file_md5.sh 
root@11.0.1.12's password: 
root@11.0.1.12's password: 
md5_b.txt                                                                                                                                                100%  541   466.3KB/s   00:00    
/app/tmp/test/10.txt changed.
/app/tmp/test/wyx.txt deleted.

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值