Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

系统文件检查在Ubuntu中运行情况有异常 #17

Open
rhchenxm opened this issue May 12, 2024 · 0 comments
Open

系统文件检查在Ubuntu中运行情况有异常 #17

rhchenxm opened this issue May 12, 2024 · 0 comments

Comments

@rhchenxm
Copy link

rhchenxm commented May 12, 2024

  • 首先,感谢写出这么好的工具,谢谢!
  • 目前版本还存在一些小瑕疵,以下是我的修改建议,供参考。
  • 系统文件检查部分的代码是指定命令的绝对路径,这在Centos类Linux里执行是OK的,但在Ubuntu里,因部分命令路径是不对的,所以运行会有异常。
  • 以下代码我加入which先得到命令路径,同时为了更直观对比系统编译时间,增加输出系统的版本和编译日期。
  • 另外加入文件大小和file格式检查内容。
    CmdFileCheck.txt
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant