文件监控脚本 – Linux bash
md5算法及生成方式
MD5值等同于文件的ID,它的值是唯一的。 如果文件已被修改(例如嵌入式病毒,特洛伊木马等),其MD5值将发生变化。 因此,一些常规下载URL提供文件MD并且通常提供MD5值。 如果用户在下载后发现他们的MD5值与网站公告不一致,可能是文件被修改过或者下载出错。
md5sum命令用于生成和校验文件的md5值。它会逐位对文件的内容进行校验。是文件的内容,与文件名无关,也就是文件内容相同,其md5值相同。md5值是一个128位的二进制数据,转换成16进制则是32(128/4)位的进制值。(生成MD5摘要的方式有很多)
majun@instance-zqtg07w6:~/bash_scripts$ md5sum /etc/passwd > passwd.md5
majun@instance-zqtg07w6:~/bash_scripts$ cat passwd.md5
709fb35cca52ac0fe8082a30976fea36 /etc/passwd
majun@instance-zqtg07w6:~/bash_scripts$ md5sum -c passwd.md5
/etc/passwd: OK #验证md5值未被更改
majun@instance-zqtg07w6:~/bash_scripts$ useradd testmd5
useradd: Permission denied.
useradd: cannot lock /etc/passwd; try again later.
majun@instance-zqtg07w6:~/bash_scripts$ exit
exit
root@instance-zqtg07w6:~# useradd testmd5
#添加用户后/etc/passwd的md5值改变
root@instance-zqtg07w6:~# su majun
majun@instance-zqtg07w6:/root$ cd ~/bash_scripts/
majun@instance-zqtg07w6:~/bash_scripts$ md5sum -c passwd.md5
/etc/passwd: FAILED # 再次验证失败
md5sum: WARNING: 1 computed checksum did NOT match
majun@instance-zqtg07w6:~/bash_scripts$
# 也可以不产生输出,静默
majun@instance-zqtg07w6:~/bash_scripts$ md5sum -c --status passwd.md5
majun@instance-zqtg07w6:~/bash_scripts$ echo $?
1
脚本
majun@instance-zqtg07w6:~/bash_scripts$ vim md5_check.sh
majun@instance-zqtg07w6:~/bash_scripts$ cat md5_check.sh
#!/bin/bash
DIRS='/tmp/csdn /home/majun/bash_scripts'
FIND=/usr/bin/find
MD5SUM=/usr/bin/md5sum
MD5_FILE="all_file.md5"
$FIND $DIRS -type f | while read line
do $MD5SUM $line >> $MD5_FILE
done
验证
majun@instance-zqtg07w6:~/bash_scripts$ bash md5_check.sh
majun@instance-zqtg07w6:~/bash_scripts$ cat all_file.md5
a870abffa168bdafbdcdb23bb2a745be /tmp/csdn/csdn.py
98c4e5c34470de352b11784cebec3461 /home/majun/bash_scripts/useradd_while02.sh
67ea669dfebf9ecf7171841cf7ea2ada /home/majun/bash_scripts/passwd.md5
ceba1c0b9883ec86385e9d7c56582b87 /home/majun/bash_scripts/server_list
7c19a2612856d048a464c0e6a4feac15 /home/majun/bash_scripts/expect_ftp.sh
ecca34e9207c19510606385cd1c1be06 /home/majun/bash_scripts/useradd_while03.sh
3a48bdf3227567f960392cd4efb7e835 /home/majun/bash_scripts/server_alive.sh
d7614256177090b89c622063c0f2048a /home/majun/bash_scripts/users.txt
8e056e480c0c7c35198abf345f87656c /home/majun/bash_scripts/md5_check.sh
17fdb0e64c8c79302e725a7f478c8fb5 /home/majun/bash_scripts/useradd_while04.sh
majun@instance-zqtg07w6:~/bash_scripts$ md5sum -c all_file.md5
# 全部没有改动所以验证ok
/tmp/csdn/csdn.py: OK
/home/majun/bash_scripts/useradd_while02.sh: OK
/home/majun/bash_scripts/passwd.md5: OK
/home/majun/bash_scripts/server_list: OK
/home/majun/bash_scripts/expect_ftp.sh: OK
/home/majun/bash_scripts/useradd_while03.sh: OK
/home/majun/bash_scripts/server_alive.sh: OK
/home/majun/bash_scripts/users.txt: OK
/home/majun/bash_scripts/md5_check.sh: OK
/home/majun/bash_scripts/useradd_while04.sh: OK
majun@instance-zqtg07w6:~/bash_scripts$
修改其中某个文件发现check不通过
majun@instance-zqtg07w6:~/bash_scripts$ echo " " >> server_list
majun@instance-zqtg07w6:~/bash_scripts$ md5sum -c all_file.md5
/tmp/csdn/csdn.py: OK
/home/majun/bash_scripts/useradd_while02.sh: OK
/home/majun/bash_scripts/passwd.md5: OK
/home/majun/bash_scripts/server_list: FAILED
/home/majun/bash_scripts/expect_ftp.sh: OK
/home/majun/bash_scripts/useradd_while03.sh: OK
/home/majun/bash_scripts/server_alive.sh: OK
/home/majun/bash_scripts/users.txt: OK
/home/majun/bash_scripts/md5_check.sh: OK
/home/majun/bash_scripts/useradd_while04.sh: OK
md5sum: WARNING: 1 computed checksum did NOT match
majun@instance-zqtg07w6:~/bash_scripts$
所有的练习脚本都在:
https://github.com/SaltNego/Learn_linux_bash