#!/bin/bash # 设置保存文件 ipaddress=$(ip address | grep -oP '(?<=inet )\d+\.\d+\.\d+\.\d+(?=\/2)' | head -n 1) filename=$ipaddress'_'$(hostname)'_'$(whoami)'_'$(date +%s)_CmdFileCheck_log'.md' print_msg() { echo -e "$1\n" | tee -a $filename } print_msg "## 文件检查" cmdline=( "which" "ifconfig" "ls" "login" "netstat" "top" "ps" "find" "grep" "passwd" "shadow" "curl" "wget" ) # 获取内核版本信息 #kernel_version=$(cat /proc/version) kernel_version=$(uname -v) print_msg "系统内核版本及编译日期:$kernel_version" print_msg "### 系统文件修改时间和大小" for cmd in "${cmdline[@]}"; do # 使用which获取命令的实际路径 full_path=$(which $cmd) if [ -n "$full_path" ]; then # 如果命令存在,获取修改时间并格式化 mod_time=$(stat -c %y "$full_path" | cut -c1-19) # formatted_time=$(date -d "$mod_time" "+%Y-%m-%d %H:%M:%S") file_size=$(du -sh "$full_path" | cut -f1) print_msg "文件:$full_path\t修改日期:$mod_time\t文件大小:$file_size" else # 如果命令不存在,打印消息 print_msg "命令 $cmd 不存在" fi done # 检查是否有file命令,如果有,获取文件类型信息 if command -v file >/dev/null 2>&1; then print_msg "### 系统文件类型" for cmd in "${cmdline[@]}"; do full_path=$(which $cmd) if [ -n "$full_path" ]; then file_type=$(file -b "$full_path") print_msg "文件:$full_path\t\t文件类型:$file_type" else print_msg "命令 $cmd 不存在" fi done else print_msg "系统无file命令,未检查系统文件类型" fi