爆款云主机2核4G限时秒杀,88元/年起!
查看详情

活动

天翼云最新优惠活动,涵盖免费试用,产品折扣等,助您降本增效!
热门活动
  • 618智算钜惠季 爆款云主机2核4G限时秒杀,88元/年起!
  • 免费体验DeepSeek,上天翼云息壤 NEW 新老用户均可免费体验2500万Tokens,限时两周
  • 云上钜惠 HOT 爆款云主机全场特惠,更有万元锦鲤券等你来领!
  • 算力套餐 HOT 让算力触手可及
  • 天翼云脑AOne NEW 连接、保护、办公,All-in-One!
  • 中小企业应用上云专场 产品组合下单即享折上9折起,助力企业快速上云
  • 息壤高校钜惠活动 NEW 天翼云息壤杯高校AI大赛,数款产品享受线上订购超值特惠
  • 天翼云电脑专场 HOT 移动办公新选择,爆款4核8G畅享1年3.5折起,快来抢购!
  • 天翼云奖励推广计划 加入成为云推官,推荐新用户注册下单得现金奖励
免费活动
  • 免费试用中心 HOT 多款云产品免费试用,快来开启云上之旅
  • 天翼云用户体验官 NEW 您的洞察,重塑科技边界

智算服务

打造统一的产品能力,实现算网调度、训练推理、技术架构、资源管理一体化智算服务
智算云(DeepSeek专区)
科研助手
  • 算力商城
  • 应用商城
  • 开发机
  • 并行计算
算力互联调度平台
  • 应用市场
  • 算力市场
  • 算力调度推荐
一站式智算服务平台
  • 模型广场
  • 体验中心
  • 服务接入
智算一体机
  • 智算一体机
大模型
  • DeepSeek-R1-昇腾版(671B)
  • DeepSeek-R1-英伟达版(671B)
  • DeepSeek-V3-昇腾版(671B)
  • DeepSeek-R1-Distill-Llama-70B
  • DeepSeek-R1-Distill-Qwen-32B
  • Qwen2-72B-Instruct
  • StableDiffusion-V2.1
  • TeleChat-12B

应用商城

天翼云精选行业优秀合作伙伴及千余款商品,提供一站式云上应用服务
进入甄选商城进入云市场创新解决方案
办公协同
  • WPS云文档
  • 安全邮箱
  • EMM手机管家
  • 智能商业平台
财务管理
  • 工资条
  • 税务风控云
企业应用
  • 翼信息化运维服务
  • 翼视频云归档解决方案
工业能源
  • 智慧工厂_生产流程管理解决方案
  • 智慧工地
建站工具
  • SSL证书
  • 新域名服务
网络工具
  • 翼云加速
灾备迁移
  • 云管家2.0
  • 翼备份
资源管理
  • 全栈混合云敏捷版(软件)
  • 全栈混合云敏捷版(一体机)
行业应用
  • 翼电子教室
  • 翼智慧显示一体化解决方案

合作伙伴

天翼云携手合作伙伴,共创云上生态,合作共赢
天翼云生态合作中心
  • 天翼云生态合作中心
天翼云渠道合作伙伴
  • 天翼云代理渠道合作伙伴
天翼云服务合作伙伴
  • 天翼云集成商交付能力认证
天翼云应用合作伙伴
  • 天翼云云市场合作伙伴
  • 天翼云甄选商城合作伙伴
天翼云技术合作伙伴
  • 天翼云OpenAPI中心
  • 天翼云EasyCoding平台
天翼云培训认证
  • 天翼云学堂
  • 天翼云市场商学院
天翼云合作计划
  • 云汇计划
天翼云东升计划
  • 适配中心
  • 东升计划
  • 适配互认证

开发者

开发者相关功能入口汇聚
技术社区
  • 专栏文章
  • 互动问答
  • 技术视频
资源与工具
  • OpenAPI中心
开放能力
  • EasyCoding敏捷开发平台
培训与认证
  • 天翼云学堂
  • 天翼云认证
魔乐社区
  • 魔乐社区

支持与服务

为您提供全方位支持与服务,全流程技术保障,助您轻松上云,安全无忧
文档与工具
  • 文档中心
  • 新手上云
  • 自助服务
  • OpenAPI中心
定价
  • 价格计算器
  • 定价策略
基础服务
  • 售前咨询
  • 在线支持
  • 在线支持
  • 工单服务
  • 建议与反馈
  • 用户体验官
  • 服务保障
  • 客户公告
  • 会员中心
增值服务
  • 红心服务
  • 首保服务
  • 客户支持计划
  • 专家技术服务
  • 备案管家

了解天翼云

天翼云秉承央企使命,致力于成为数字经济主力军,投身科技强国伟大事业,为用户提供安全、普惠云服务
品牌介绍
  • 关于天翼云
  • 智算云
  • 天翼云4.0
  • 新闻资讯
  • 天翼云APP
基础设施
  • 全球基础设施
  • 信任中心
最佳实践
  • 精选案例
  • 超级探访
  • 云杂志
  • 分析师和白皮书
  • 天翼云·创新直播间
市场活动
  • 2025智能云生态大会
  • 2024智算云生态大会
  • 2023云生态大会
  • 2022云生态大会
  • 天翼云中国行
天翼云
  • 活动
  • 智算服务
  • 产品
  • 解决方案
  • 应用商城
  • 合作伙伴
  • 开发者
  • 支持与服务
  • 了解天翼云
      • 文档
      • 控制中心
      • 备案
      • 管理中心

      Linux 系统日常巡检脚本 干货

      首页 知识中心 服务器 文章详情页

      Linux 系统日常巡检脚本 干货

      2024-11-20 09:46:57 阅读次数:127

      centos,linux

      Linux 系统日常巡检脚本,巡检内容包含了,磁盘,内存 cpu 进程 文件更改 用户登录等一系列的操作 直接用就行了

      报告以邮件发送到邮箱 在log下生成巡检报告。

      #!/bin/bash
      # @Author: HanWei
      # @Date: 2020-03-16 09:56:57
      # @Last Modified time: 2020-03-16 11:06:31
      #!/bin/bash
      #主机信息每日巡检

      IPADDR=$(ifconfig eth0|grep 'inet addr'|awk -F '[ :]' '{print $13}')
      #环境变量PATH没设好,在cron里执行时有很多命令会找不到
      export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
      source /etc/profile

      [ $(id -u) -gt 0 ] && echo "请用root用户执行此脚本!" && exit 1
      centosVersion=$(awk '{print $(NF-1)}' /etc/redhat-release)
      VERSION="2020-03-16"

      #日志相关
      PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
      [ -f $PROGPATH ] && PROGPATH="."
      LOGPATH="$PROGPATH/log"
      [ -e $LOGPATH ] || mkdir $LOGPATH
      RESULTFILE="$LOGPATH/HostDailyCheck-$IPADDR-`date +%Y%m%d`.txt"


      #定义报表的全局变量
      report_DateTime="" #日期 ok
      report_Hostname="" #主机名 ok
      report_OSRelease="" #发行版本 ok
      report_Kernel="" #内核 ok
      report_Language="" #语言/编码 ok
      report_LastReboot="" #最近启动时间 ok
      report_Uptime="" #运行时间(天) ok
      report_CPUs="" #CPU数量 ok
      report_CPUType="" #CPU类型 ok
      report_Arch="" #CPU架构 ok
      report_MemTotal="" #内存总容量(MB) ok
      report_MemFree="" #内存剩余(MB) ok
      report_MemUsedPercent="" #内存使用率% ok
      report_DiskTotal="" #硬盘总容量(GB) ok
      report_DiskFree="" #硬盘剩余(GB) ok
      report_DiskUsedPercent="" #硬盘使用率% ok
      report_InodeTotal="" #Inode总量 ok
      report_InodeFree="" #Inode剩余 ok
      report_InodeUsedPercent="" #Inode使用率 ok
      report_IP="" #IP地址 ok
      report_MAC="" #MAC地址 ok
      report_Gateway="" #默认网关 ok
      report_DNS="" #DNS ok
      report_Listen="" #监听 ok
      report_Selinux="" #Selinux ok
      report_Firewall="" #防火墙 ok
      report_USERs="" #用户 ok
      report_USEREmptyPassword="" #空密码用户 ok
      report_USERTheSameUID="" #相同ID的用户 ok
      report_PasswordExpiry="" #密码过期(天) ok
      report_RootUser="" #root用户 ok
      report_Sudoers="" #sudo授权 ok
      report_SSHAuthorized="" #SSH信任主机 ok
      report_SSHDProtocolVersion="" #SSH协议版本 ok
      report_SSHDPermitRootLogin="" #允许root远程登录 ok
      report_DefunctProsess="" #僵尸进程数量 ok
      report_SelfInitiatedService="" #自启动服务数量 ok
      report_SelfInitiatedProgram="" #自启动程序数量 ok
      report_RuningService="" #运行中服务数 ok
      report_Crontab="" #计划任务数 ok
      report_Syslog="" #日志服务 ok
      report_SNMP="" #SNMP OK
      report_NTP="" #NTP ok
      report_JDK="" #JDK版本 ok
      function version(){
      echo ""
      echo ""
      echo "系统巡检脚本:Version $VERSION"
      }

      function getCpuStatus(){
      echo ""
      echo ""
      echo "############################ CPU检查 #############################"
      Physical_CPUs=$(grep "physical id" /proc/cpuinfo| sort | uniq | wc -l)
      Virt_CPUs=$(grep "processor" /proc/cpuinfo | wc -l)
      CPU_Kernels=$(grep "cores" /proc/cpuinfo|uniq| awk -F ': ' '{print $2}')
      CPU_Type=$(grep "model name" /proc/cpuinfo | awk -F ': ' '{print $2}' | sort | uniq)
      CPU_Arch=$(uname -m)
      echo "物理CPU个数:$Physical_CPUs"
      echo "逻辑CPU个数:$Virt_CPUs"
      echo "每CPU核心数:$CPU_Kernels"
      echo " CPU型号:$CPU_Type"
      echo " CPU架构:$CPU_Arch"
      #报表信息
      report_CPUs=$Virt_CPUs #CPU数量
      report_CPUType=$CPU_Type #CPU类型
      report_Arch=$CPU_Arch #CPU架构
      }

      function getMemStatus(){
      echo ""
      echo ""
      echo "############################ 内存检查 ############################"
      if [[ $centosVersion < 7 ]];then
      free -mo
      else
      free -h
      fi
      #报表信息
      MemTotal=$(grep MemTotal /proc/meminfo| awk '{print $2}') #KB
      MemFree=$(grep MemFree /proc/meminfo| awk '{print $2}') #KB
      let MemUsed=MemTotal-MemFree
      MemPercent=$(awk "BEGIN {if($MemTotal==0){printf 100}else{printf \"%.2f\",$MemUsed*100/$MemTotal}}")
      report_MemTotal="$((MemTotal/1024))""MB" #内存总容量(MB)
      report_MemFree="$((MemFree/1024))""MB" #内存剩余(MB)
      report_MemUsedPercent="$(awk "BEGIN {if($MemTotal==0){printf 100}else{printf \"%.2f\",$MemUsed*100/$MemTotal}}")""%" #内存使用率%
      }
      function getDiskStatus(){
      echo ""
      echo ""
      echo "############################ 磁盘检查 ############################"
      df -hiP | sed 's/Mounted on/Mounted/'> /tmp/inode
      df -hTP | sed 's/Mounted on/Mounted/'> /tmp/disk
      join /tmp/disk /tmp/inode | awk '{print $1,$2,"|",$3,$4,$5,$6,"|",$8,$9,$10,$11,"|",$12}'| column -t
      #报表信息
      diskdata=$(df -TP | sed '1d' | awk '$2!="tmpfs"{print}') #KB
      disktotal=$(echo "$diskdata" | awk '{total+=$3}END{print total}') #KB
      diskused=$(echo "$diskdata" | awk '{total+=$4}END{print total}') #KB
      diskfree=$((disktotal-diskused)) #KB
      diskusedpercent=$(echo $disktotal $diskused | awk '{if($1==0){printf 100}else{printf "%.2f",$2*100/$1}}')
      inodedata=$(df -iTP | sed '1d' | awk '$2!="tmpfs"{print}')
      inodetotal=$(echo "$inodedata" | awk '{total+=$3}END{print total}')
      inodeused=$(echo "$inodedata" | awk '{total+=$4}END{print total}')
      inodefree=$((inodetotal-inodeused))
      inodeusedpercent=$(echo $inodetotal $inodeused | awk '{if($1==0){printf 100}else{printf "%.2f",$2*100/$1}}')
      report_DiskTotal=$((disktotal/1024/1024))"GB" #硬盘总容量(GB)
      report_DiskFree=$((diskfree/1024/1024))"GB" #硬盘剩余(GB)
      report_DiskUsedPercent="$diskusedpercent""%" #硬盘使用率%
      report_InodeTotal=$((inodetotal/1000))"K" #Inode总量
      report_InodeFree=$((inodefree/1000))"K" #Inode剩余
      report_InodeUsedPercent="$inodeusedpercent""%" #Inode使用率%

      }

      function getSystemStatus(){
      echo ""
      echo ""
      echo "############################ 系统检查 ############################"
      if [ -e /etc/sysconfig/i18n ];then
      default_LANG="$(grep "LANG=" /etc/sysconfig/i18n | grep -v "^#" | awk -F '"' '{print $2}')"
      else
      default_LANG=$LANG
      fi
      export LANG="en_US.UTF-8"
      Release=$(cat /etc/redhat-release 2>/dev/null)
      Kernel=$(uname -r)
      OS=$(uname -o)
      Hostname=$(uname -n)
      SELinux=$(/usr/sbin/sestatus | grep "SELinux status: " | awk '{print $3}')
      LastReboot=$(who -b | awk '{print $3,$4}')
      uptime=$(uptime | sed 's/.*up \([^,]*\), .*/\1/')
      echo " 系统:$OS"
      echo " 发行版本:$Release"
      echo " 内核:$Kernel"
      echo " 主机名:$Hostname"
      echo " SELinux:$SELinux"
      echo "语言/编码:$default_LANG"
      echo " 当前时间:$(date +'%F %T')"
      echo " 最后启动:$LastReboot"
      echo " 运行时间:$uptime"
      #报表信息
      report_DateTime=$(date +"%F %T") #日期
      report_Hostname="$Hostname" #主机名
      report_OSRelease="$Release" #发行版本
      report_Kernel="$Kernel" #内核
      report_Language="$default_LANG" #语言/编码
      report_LastReboot="$LastReboot" #最近启动时间
      report_Uptime="$uptime" #运行时间(天)
      report_Selinux="$SELinux"
      export LANG="$default_LANG"

      }

      function getServiceStatus(){
      echo ""
      echo ""
      echo "############################ 服务检查 ############################"
      echo ""
      if [[ $centosVersion > 7 ]];then
      conf=$(systemctl list-unit-files --type=service --state=enabled --no-pager | grep "enabled")
      process=$(systemctl list-units --type=service --state=running --no-pager | grep ".service")
      #报表信息
      report_SelfInitiatedService="$(echo "$conf" | wc -l)" #自启动服务数量
      report_RuningService="$(echo "$process" | wc -l)" #运行中服务数量
      else
      conf=$(/sbin/chkconfig | grep -E ":on|:启用")
      process=$(/sbin/service --status-all 2>/dev/null | grep -E "is running|正在运行")
      #报表信息
      report_SelfInitiatedService="$(echo "$conf" | wc -l)" #自启动服务数量
      report_RuningService="$(echo "$process" | wc -l)" #运行中服务数量
      fi
      echo "服务配置"
      echo "--------"
      echo "$conf" | column -t
      echo ""
      echo "正在运行的服务"
      echo "--------------"
      echo "$process"

      }


      function getAutoStartStatus(){
      echo ""
      echo ""
      echo "############################ 自启动检查 ##########################"
      conf=$(grep -v "^#" /etc/rc.d/rc.local| sed '/^$/d')
      echo "$conf"
      #报表信息
      report_SelfInitiatedProgram="$(echo $conf | wc -l)" #自启动程序数量
      }

      function getLoginStatus(){
      echo ""
      echo ""
      echo "############################ 登录检查 ############################"
      last | head
      }

      function getNetworkStatus(){
      echo ""
      echo ""
      echo "############################ 网络检查 ############################"
      if [[ $centosVersion < 7 ]];then
      /sbin/ifconfig -a | grep -v packets | grep -v collisions | grep -v inet6
      else
      #ip a
      for i in $(ip link | grep BROADCAST | awk -F: '{print $2}');do ip add show $i | grep -E "BROADCAST|global"| awk '{print $2}' | tr '\n' ' ' ;echo "" ;done
      fi
      GATEWAY=$(ip route | grep default | awk '{print $3}')
      DNS=$(grep nameserver /etc/resolv.conf| grep -v "#" | awk '{print $2}' | tr '\n' ',' | sed 's/,$//')
      echo ""
      echo "网关:$GATEWAY "
      echo " DNS:$DNS"
      #报表信息
      IP=$(ip -f inet addr | grep -v 127.0.0.1 | grep inet | awk '{print $NF,$2}' | tr '\n' ',' | sed 's/,$//')
      MAC=$(ip link | grep -v "LOOPBACK\|loopback" | awk '{print $2}' | sed 'N;s/\n//' | tr '\n' ',' | sed 's/,$//')
      report_IP="$IP" #IP地址
      report_MAC=$MAC #MAC地址
      report_Gateway="$GATEWAY" #默认网关
      report_DNS="$DNS" #DNS
      }

      function getListenStatus(){
      echo ""
      echo ""
      echo "############################ 监听检查 ############################"
      TCPListen=$(ss -ntul | column -t)
      echo "$TCPListen"
      #报表信息
      report_Listen="$(echo "$TCPListen"| sed '1d' | awk '/tcp/ {print $5}' | awk -F: '{print $NF}' | sort | uniq | wc -l)"
      }

      function getCronStatus(){
      echo ""
      echo ""
      echo "############################ 计划任务检查 ########################"
      Crontab=0
      for shell in $(grep -v "/sbin/nologin" /etc/shells);do
      for user in $(grep "$shell" /etc/passwd| awk -F: '{print $1}');do
      crontab -l -u $user >/dev/null 2>&1
      status=$?
      if [ $status -eq 0 ];then
      echo "$user"
      echo "--------"
      crontab -l -u $user
      let Crontab=Crontab+$(crontab -l -u $user | wc -l)
      echo ""
      fi
      done
      done
      #计划任务
      find /etc/cron* -type f | xargs -i ls -l {} | column -t
      let Crontab=Crontab+$(find /etc/cron* -type f | wc -l)
      #报表信息
      report_Crontab="$Crontab" #计划任务数
      }
      function getHowLongAgo(){
      # 计算一个时间戳离现在有多久了
      datetime="$*"
      [ -z "$datetime" ] && echo "错误的参数:getHowLongAgo() $*"
      Timestamp=$(date +%s -d "$datetime") #转化为时间戳
      Now_Timestamp=$(date +%s)
      Difference_Timestamp=$(($Now_Timestamp-$Timestamp))
      days=0;hours=0;minutes=0;
      sec_in_day=$((60*60*24));
      sec_in_hour=$((60*60));
      sec_in_minute=60
      while (( $(($Difference_Timestamp-$sec_in_day)) > 1 ))
      do
      let Difference_Timestamp=Difference_Timestamp-sec_in_day
      let days++
      done
      while (( $(($Difference_Timestamp-$sec_in_hour)) > 1 ))
      do
      let Difference_Timestamp=Difference_Timestamp-sec_in_hour
      let hours++
      done
      echo "$days 天 $hours 小时前"
      }

      function getUserLastLogin(){
      # 获取用户最近一次登录的时间,含年份
      # 很遗憾last命令不支持显示年份,只有"last -t YYYYMMDDHHMMSS"表示某个时间之间的登录,我
      # 们只能用最笨的方法了,对比今天之前和今年元旦之前(或者去年之前和前年之前……)某个用户
      # 登录次数,如果登录统计次数有变化,则说明最近一次登录是今年。
      username=$1
      : ${username:="`whoami`"}
      thisYear=$(date +%Y)
      oldesYear=$(last | tail -n1 | awk '{print $NF}')
      while(( $thisYear >= $oldesYear));do
      loginBeforeToday=$(last $username | grep $username | wc -l)
      loginBeforeNewYearsDayOfThisYear=$(last $username -t $thisYear"0101000000" | grep $username | wc -l)
      if [ $loginBeforeToday -eq 0 ];then
      echo "从未登录过"
      break
      elif [ $loginBeforeToday -gt $loginBeforeNewYearsDayOfThisYear ];then
      lastDateTime=$(last -i $username | head -n1 | awk '{for(i=4;i<(NF-2);i++)printf"%s ",$i}')" $thisYear" #格式如: Sat Nov 2 20:33 2015
      lastDateTime=$(date "+%Y-%m-%d %H:%M:%S" -d "$lastDateTime")
      echo "$lastDateTime"
      break
      else
      thisYear=$((thisYear-1))
      fi
      done

      }

      function getUserStatus(){
      echo ""
      echo ""
      echo "############################ 用户检查 ############################"
      #/etc/passwd 最后修改时间
      pwdfile="$(cat /etc/passwd)"
      Modify=$(stat /etc/passwd | grep Modify | tr '.' ' ' | awk '{print $2,$3}')

      echo "/etc/passwd 最后修改时间:$Modify ($(getHowLongAgo $Modify))"
      echo ""
      echo "特权用户"
      echo "--------"
      RootUser=""
      for user in $(echo "$pwdfile" | awk -F: '{print $1}');do
      if [ $(id -u $user) -eq 0 ];then
      echo "$user"
      RootUser="$RootUser,$user"
      fi
      done
      echo ""
      echo "用户列表"
      echo "--------"
      USERs=0
      echo "$(
      echo "用户名 UID GID HOME SHELL 最后一次登录"
      for shell in $(grep -v "/sbin/nologin" /etc/shells);do
      for username in $(grep "$shell" /etc/passwd| awk -F: '{print $1}');do
      userLastLogin="$(getUserLastLogin $username)"
      echo "$pwdfile" | grep -w "$username" |grep -w "$shell"| awk -F: -v lastlogin="$(echo "$userLastLogin" | tr ' ' '_')" '{print $1,$3,$4,$6,$7,lastlogin}'
      done
      let USERs=USERs+$(echo "$pwdfile" | grep "$shell"| wc -l)
      done
      )" | column -t
      echo ""
      echo "空密码用户"
      echo "----------"
      USEREmptyPassword=""
      for shell in $(grep -v "/sbin/nologin" /etc/shells);do
      for user in $(echo "$pwdfile" | grep "$shell" | cut -d: -f1);do
      r=$(awk -F: '$2=="!!"{print $1}' /etc/shadow | grep -w $user)
      if [ ! -z $r ];then
      echo $r
      USEREmptyPassword="$USEREmptyPassword,"$r
      fi
      done
      done
      echo ""
      echo "相同ID的用户"
      echo "------------"
      USERTheSameUID=""
      UIDs=$(cut -d: -f3 /etc/passwd | sort | uniq -c | awk '$1>1{print $2}')
      for uid in $UIDs;do
      echo -n "$uid";
      USERTheSameUID="$uid"
      r=$(awk -F: 'ORS="";$3=='"$uid"'{print ":",$1}' /etc/passwd)
      echo "$r"
      echo ""
      USERTheSameUID="$USERTheSameUID $r,"
      done
      #报表信息
      report_USERs="$USERs" #用户
      report_USEREmptyPassword=$(echo $USEREmptyPassword | sed 's/^,//')
      report_USERTheSameUID=$(echo $USERTheSameUID | sed 's/,$//')
      report_RootUser=$(echo $RootUser | sed 's/^,//') #特权用户
      }


      function getPasswordStatus {
      echo ""
      echo ""
      echo "############################ 密码检查 ############################"
      pwdfile="$(cat /etc/passwd)"
      echo ""
      echo "密码过期检查"
      echo "------------"
      result=""
      for shell in $(grep -v "/sbin/nologin" /etc/shells);do
      for user in $(echo "$pwdfile" | grep "$shell" | cut -d: -f1);do
      get_expiry_date=$(/usr/bin/chage -l $user | grep 'Password expires' | cut -d: -f2)
      if [[ $get_expiry_date = ' never' || $get_expiry_date = 'never' ]];then
      printf "%-15s 永不过期\n" $user
      result="$result,$user:never"
      else
      password_expiry_date=$(date -d "$get_expiry_date" "+%s")
      current_date=$(date "+%s")
      diff=$(($password_expiry_date-$current_date))
      let DAYS=$(($diff/(60*60*24)))
      printf "%-15s %s天后过期\n" $user $DAYS
      result="$result,$user:$DAYS days"
      fi
      done
      done
      report_PasswordExpiry=$(echo $result | sed 's/^,//')

      echo ""
      echo "密码策略检查"
      echo "------------"
      grep -v "#" /etc/login.defs | grep -E "PASS_MAX_DAYS|PASS_MIN_DAYS|PASS_MIN_LEN|PASS_WARN_AGE"


      }

      function getSudoersStatus(){
      echo ""
      echo ""
      echo "############################ Sudoers检查 #########################"
      conf=$(grep -v "^#" /etc/sudoers| grep -v "^Defaults" | sed '/^$/d')
      echo "$conf"
      echo ""
      #报表信息
      report_Sudoers="$(echo $conf | wc -l)"
      }

      function getInstalledStatus(){
      echo ""
      echo ""
      echo "############################ 软件检查 ############################"
      rpm -qa --last | head | column -t
      }

      function getProcessStatus(){
      echo ""
      echo ""
      echo "############################ 进程检查 ############################"
      if [ $(ps -ef | grep defunct | grep -v grep | wc -l) -ge 1 ];then
      echo ""
      echo "僵尸进程";
      echo "--------"
      ps -ef | head -n1
      ps -ef | grep defunct | grep -v grep
      fi
      echo ""
      echo "内存占用TOP10"
      echo "-------------"
      echo -e "PID %MEM RSS COMMAND
      $(ps aux | awk '{print $2, $4, $6, $11}' | sort -k3rn | head -n 10 )"| column -t
      echo ""
      echo "CPU占用TOP10"
      echo "------------"
      top b -n1 | head -17 | tail -11
      #报表信息
      report_DefunctProsess="$(ps -ef | grep defunct | grep -v grep|wc -l)"
      }

      function getJDKStatus(){
      echo ""
      echo ""
      echo "############################ JDK检查 #############################"
      java -version 2>/dev/null
      if [ $? -eq 0 ];then
      java -version 2>&1
      fi
      echo "JAVA_HOME=\"$JAVA_HOME\""
      #报表信息
      report_JDK="$(java -version 2>&1 | grep version | awk '{print $1,$3}' | tr -d '"')"
      }
      function getSyslogStatus(){
      echo ""
      echo ""
      echo "############################ syslog检查 ##########################"
      echo "服务状态:$(getState rsyslog)"
      echo ""
      echo "/etc/rsyslog.conf"
      echo "-----------------"
      cat /etc/rsyslog.conf 2>/dev/null | grep -v "^#" | grep -v "^\\$" | sed '/^$/d' | column -t
      #报表信息
      report_Syslog="$(getState rsyslog)"
      }
      function getFirewallStatus(){
      echo ""
      echo ""
      echo "############################ 防火墙检查 ##########################"
      #防火墙状态,策略等
      if [[ $centosVersion < 7 ]];then
      /etc/init.d/iptables status >/dev/null 2>&1
      status=$?
      if [ $status -eq 0 ];then
      s="active"
      elif [ $status -eq 3 ];then
      s="inactive"
      elif [ $status -eq 4 ];then
      s="permission denied"
      else
      s="unknown"
      fi
      else
      s="$(getState iptables)"
      fi
      echo "iptables: $s"
      echo ""
      echo "/etc/sysconfig/iptables"
      echo "-----------------------"
      cat /etc/sysconfig/iptables 2>/dev/null
      #报表信息
      report_Firewall="$s"
      }

      function getSNMPStatus(){
      #SNMP服务状态,配置等
      echo ""
      echo ""
      echo "############################ SNMP检查 ############################"
      status="$(getState snmpd)"
      echo "服务状态:$status"
      echo ""
      if [ -e /etc/snmp/snmpd.conf ];then
      echo "/etc/snmp/snmpd.conf"
      echo "--------------------"
      cat /etc/snmp/snmpd.conf 2>/dev/null | grep -v "^#" | sed '/^$/d'
      fi
      #报表信息
      report_SNMP="$(getState snmpd)"
      }



      function getState(){
      if [[ $centosVersion < 7 ]];then
      if [ -e "/etc/init.d/$1" ];then
      if [ `/etc/init.d/$1 status 2>/dev/null | grep -E "is running|正在运行" | wc -l` -ge 1 ];then
      r="active"
      else
      r="inactive"
      fi
      else
      r="unknown"
      fi
      else
      #CentOS 7+
      r="$(systemctl is-active $1 2>&1)"
      fi
      echo "$r"
      }

      function getSSHStatus(){
      #SSHD服务状态,配置,受信任主机等
      echo ""
      echo ""
      echo "############################ SSH检查 #############################"
      #检查受信任主机
      pwdfile="$(cat /etc/passwd)"
      echo "服务状态:$(getState sshd)"
      Protocol_Version=$(cat /etc/ssh/sshd_config | grep Protocol | awk '{print $2}')
      echo "SSH协议版本:$Protocol_Version"
      echo ""
      echo "信任主机"
      echo "--------"
      authorized=0
      for user in $(echo "$pwdfile" | grep /bin/bash | awk -F: '{print $1}');do
      authorize_file=$(echo "$pwdfile" | grep -w $user | awk -F: '{printf $6"/.ssh/authorized_keys"}')
      authorized_host=$(cat $authorize_file 2>/dev/null | awk '{print $3}' | tr '\n' ',' | sed 's/,$//')
      if [ ! -z $authorized_host ];then
      echo "$user 授权 \"$authorized_host\" 无密码访问"
      fi
      let authorized=authorized+$(cat $authorize_file 2>/dev/null | awk '{print $3}'|wc -l)
      done

      echo ""
      echo "是否允许ROOT远程登录"
      echo "--------------------"
      config=$(cat /etc/ssh/sshd_config | grep PermitRootLogin)
      firstChar=${config:0:1}
      if [ $firstChar == "#" ];then
      PermitRootLogin="yes" #默认是允许ROOT远程登录的
      else
      PermitRootLogin=$(echo $config | awk '{print $2}')
      fi
      echo "PermitRootLogin $PermitRootLogin"

      echo ""
      echo "/etc/ssh/sshd_config"
      echo "--------------------"
      cat /etc/ssh/sshd_config | grep -v "^#" | sed '/^$/d'

      #报表信息
      report_SSHAuthorized="$authorized" #SSH信任主机
      report_SSHDProtocolVersion="$Protocol_Version" #SSH协议版本
      report_SSHDPermitRootLogin="$PermitRootLogin" #允许root远程登录
      }
      function getNTPStatus(){
      #NTP服务状态,当前时间,配置等
      echo ""
      echo ""
      echo "############################ NTP检查 #############################"
      if [ -e /etc/ntp.conf ];then
      echo "服务状态:$(getState ntpd)"
      echo ""
      echo "/etc/ntp.conf"
      echo "-------------"
      cat /etc/ntp.conf 2>/dev/null | grep -v "^#" | sed '/^$/d'
      fi
      #报表信息
      report_NTP="$(getState ntpd)"
      }


      function uploadHostDailyCheckReport(){
      json="{
      \"DateTime\":\"$report_DateTime\",
      \"Hostname\":\"$report_Hostname\",
      \"OSRelease\":\"$report_OSRelease\",
      \"Kernel\":\"$report_Kernel\",
      \"Language\":\"$report_Language\",
      \"LastReboot\":\"$report_LastReboot\",
      \"Uptime\":\"$report_Uptime\",
      \"CPUs\":\"$report_CPUs\",
      \"CPUType\":\"$report_CPUType\",
      \"Arch\":\"$report_Arch\",
      \"MemTotal\":\"$report_MemTotal\",
      \"MemFree\":\"$report_MemFree\",
      \"MemUsedPercent\":\"$report_MemUsedPercent\",
      \"DiskTotal\":\"$report_DiskTotal\",
      \"DiskFree\":\"$report_DiskFree\",
      \"DiskUsedPercent\":\"$report_DiskUsedPercent\",
      \"InodeTotal\":\"$report_InodeTotal\",
      \"InodeFree\":\"$report_InodeFree\",
      \"InodeUsedPercent\":\"$report_InodeUsedPercent\",
      \"IP\":\"$report_IP\",
      \"MAC\":\"$report_MAC\",
      \"Gateway\":\"$report_Gateway\",
      \"DNS\":\"$report_DNS\",
      \"Listen\":\"$report_Listen\",
      \"Selinux\":\"$report_Selinux\",
      \"Firewall\":\"$report_Firewall\",
      \"USERs\":\"$report_USERs\",
      \"USEREmptyPassword\":\"$report_USEREmptyPassword\",
      \"USERTheSameUID\":\"$report_USERTheSameUID\",
      \"PasswordExpiry\":\"$report_PasswordExpiry\",
      \"RootUser\":\"$report_RootUser\",
      \"Sudoers\":\"$report_Sudoers\",
      \"SSHAuthorized\":\"$report_SSHAuthorized\",
      \"SSHDProtocolVersion\":\"$report_SSHDProtocolVersion\",
      \"SSHDPermitRootLogin\":\"$report_SSHDPermitRootLogin\",
      \"DefunctProsess\":\"$report_DefunctProsess\",
      \"SelfInitiatedService\":\"$report_SelfInitiatedService\",
      \"SelfInitiatedProgram\":\"$report_SelfInitiatedProgram\",
      \"RuningService\":\"$report_RuningService\",
      \"Crontab\":\"$report_Crontab\",
      \"Syslog\":\"$report_Syslog\",
      \"SNMP\":\"$report_SNMP\",
      \"NTP\":\"$report_NTP\",
      \"JDK\":\"$report_JDK\"
      }"
      #echo "$json"
      curl -l -H "Content-type: application/json" -X POST -d "$json" "$uploadHostDailyCheckReportApi" 2>/dev/null
      }

      function getchage_file_24h()
      {
      echo "############################ 文件检查 #############################"
      check2=$(find / -name '*.sh' -mtime -1)
      check21=$(find / -name '*.asp' -mtime -1)
      check22=$(find / -name '*.php' -mtime -1)
      check23=$(find / -name '*.aspx' -mtime -1)
      check24=$(find / -name '*.jsp' -mtime -1)
      check25=$(find / -name '*.html' -mtime -1)
      check26=$(find / -name '*.htm' -mtime -1)
      check9=$(find / -name core -exec ls -l {} \;)
      check10=$(cat /etc/crontab)
      check12=$(ls -alt /usr/bin | head -10)
      cat <<EOF

      ############################查看所有被修改过的文件返回最近24小时内的############################
      ${check2}
      ${check21}
      ${check22}
      ${check23}
      ${check24}
      ${check25}
      ${check26}
      ${line}

      ############################检查定时文件的完整性############################
      ${check10}
      ${line}

      ############################查看系统命令是否被替换############################
      ${check12}
      ${line}
      EOF
      }

      function check(){
      version
      getSystemStatus
      getCpuStatus
      getMemStatus
      getDiskStatus
      getNetworkStatus
      getListenStatus
      getProcessStatus
      getServiceStatus
      getAutoStartStatus
      getLoginStatus
      getCronStatus
      getUserStatus
      getPasswordStatus
      getSudoersStatus
      getJDKStatus
      getFirewallStatus
      getSSHStatus
      getSyslogStatus
      getSNMPStatus
      getNTPStatus
      getInstalledStatus
      getchage_file_24h
      }


      #执行检查并保存检查结果
      check > $RESULTFILE

      echo "检查结果:$RESULTFILE"
      echo -e "`date "+%Y-%m-%d %H:%M:%S"` 阿里云PHP企业平台巡检报告" | mail -a $RESULTFILE -s "阿里云PHP企业平台巡检报告" h@
      版权声明:本文内容来自第三方投稿或授权转载,原文地址:https://blog.51cto.com/wangshiyu/4940883,作者:JavaPub,版权归原作者所有。本网站转在其作品的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如因作品内容、版权等问题需要同本网站联系,请发邮件至ctyunbbs@chinatelecom.cn沟通。

      上一篇:Git生成SSH公钥----CentOS7系统

      下一篇:服务器数据恢复—某品牌P2000服务器RAID5阵列硬盘无法识别,序列号无法读取的数据恢复案例

      相关文章

      2025-05-19 09:04:53

      查看RISC-V版本的gcc中默认定义的宏

      查看RISC-V版本的gcc中默认定义的宏

      2025-05-19 09:04:53
      c++ , linux
      2025-04-01 10:28:37

      小课2:筛选信息命令

      小课2:筛选信息命令

      2025-04-01 10:28:37
      bash , linux , 升序 , 服务器 , 运维
      2025-03-26 09:31:12

      shell脚本实现查询代码中定义了多少宏的方法

      shell脚本实现查询代码中定义了多少宏的方法

      2025-03-26 09:31:12
      bash , linux , 运维
      2025-03-06 09:15:26

      spring cloud系统安装涉及的技术说明

      spring cloud系统安装涉及的技术说明

      2025-03-06 09:15:26
      docker , linux , 安装 , 技术
      2025-03-05 09:24:43

      【Python】使用numpy库实现Tic-Tac-Toe井字棋

      【Python】使用numpy库实现Tic-Tac-Toe井字棋

      2025-03-05 09:24:43
      linux , 右键 , 安装 , 打开 , 输入
      2025-02-10 08:53:59

      【linux】linux C 程序 注册信号处理函数

      【linux】linux C 程序 注册信号处理函数  

      2025-02-10 08:53:59
      linux , 函数 , 注册 , 程序
      2025-01-17 09:07:21

      课时3:处理信息命令

      课时3:处理信息命令

      2025-01-17 09:07:21
      linux , shell , 数据库 , 服务器 , 运维
      2024-12-06 06:39:06

      SpringBoot项目在linux下部署脚本实例

      SpringBoot项目在linux下部署脚本实例

      2024-12-06 06:39:06
      linux , 示例 , 脚本
      2024-11-20 09:46:57

      Linux 系统日常巡检脚本

      Linux 系统日常巡检脚本,巡检内容包含了,磁盘,内存 cpu 进程 文件更改 用户登录等一系列的操作 直接用就行了

      2024-11-20 09:46:57
      java , linux
      2024-11-20 09:46:57

      提权工具推荐(PEASS-ng、linpeas_linux_amd64、winPEASany_ofs)

      提权工具推荐(PEASS-ng、linpeas_linux_amd64、winPEASany_ofs)

      2024-11-20 09:46:57
      linux , 服务器 , 运维
      查看更多
      推荐标签

      作者介绍

      天翼云小翼
      天翼云用户

      文章

      33561

      阅读量

      5223727

      查看更多

      最新文章

      小课2:筛选信息命令

      2025-04-01 10:28:37

      课时3:处理信息命令

      2025-01-17 09:07:21

      Linux 系统日常巡检脚本

      2024-11-20 09:46:57

      提权工具推荐(PEASS-ng、linpeas_linux_amd64、winPEASany_ofs)

      2024-11-20 09:46:57

      Git生成SSH公钥----CentOS7系统

      2024-11-20 09:46:40

      LINUX的基本管理(1.0)

      2024-11-18 09:54:27

      查看更多

      热门文章

      linux篇-linux iptables配置

      2023-03-16 06:47:52

      Linux运维小技巧---每日收集所有服务器信息并归档到指定服务器

      2023-03-16 07:49:58

      linux中常见工具安装问题集锦

      2023-05-05 10:12:49

      linux环境日志排查,cat命令关键字查找、最近1000条、定位到指定位置

      2022-12-28 07:22:30

      小型自动化运维--expect脚本V2版

      2023-05-16 09:38:32

      Mac 终端连接linux程服务器并相互传输文件

      2023-04-23 09:44:23

      查看更多

      热门标签

      服务器 linux 虚拟机 Linux 数据库 运维 网络 日志 数据恢复 java python 配置 nginx centos mysql
      查看更多

      相关产品

      弹性云主机

      随时自助获取、弹性伸缩的云服务器资源

      天翼云电脑(公众版)

      便捷、安全、高效的云电脑服务

      对象存储

      高品质、低成本的云上存储服务

      云硬盘

      为云上计算资源提供持久性块存储

      查看更多

      随机文章

      (linux-x86)openEuler安装配置OpenJDK1.8

      Linux系统关闭或重新启动主机的命令详解

      【Linux】Vmware虚拟机安装Redhat 8.6

      在Ubuntu Kylin系统中安装并使用minicom

      vmware的vmdk格式虚拟机转换为kvm的qcow2格式

      scp 命令

      • 7*24小时售后
      • 无忧退款
      • 免费备案
      • 专家服务
      售前咨询热线
      400-810-9889转1
      关注天翼云
      • 旗舰店
      • 天翼云APP
      • 天翼云微信公众号
      服务与支持
      • 备案中心
      • 售前咨询
      • 智能客服
      • 自助服务
      • 工单管理
      • 客户公告
      • 涉诈举报
      账户管理
      • 管理中心
      • 订单管理
      • 余额管理
      • 发票管理
      • 充值汇款
      • 续费管理
      快速入口
      • 天翼云旗舰店
      • 文档中心
      • 最新活动
      • 免费试用
      • 信任中心
      • 天翼云学堂
      云网生态
      • 甄选商城
      • 渠道合作
      • 云市场合作
      了解天翼云
      • 关于天翼云
      • 天翼云APP
      • 服务案例
      • 新闻资讯
      • 联系我们
      热门产品
      • 云电脑
      • 弹性云主机
      • 云电脑政企版
      • 天翼云手机
      • 云数据库
      • 对象存储
      • 云硬盘
      • Web应用防火墙
      • 服务器安全卫士
      • CDN加速
      热门推荐
      • 云服务备份
      • 边缘安全加速平台
      • 全站加速
      • 安全加速
      • 云服务器
      • 云主机
      • 智能边缘云
      • 应用编排服务
      • 微服务引擎
      • 共享流量包
      更多推荐
      • web应用防火墙
      • 密钥管理
      • 等保咨询
      • 安全专区
      • 应用运维管理
      • 云日志服务
      • 文档数据库服务
      • 云搜索服务
      • 数据湖探索
      • 数据仓库服务
      友情链接
      • 中国电信集团
      • 189邮箱
      • 天翼企业云盘
      • 天翼云盘
      ©2025 天翼云科技有限公司版权所有 增值电信业务经营许可证A2.B1.B2-20090001
      公司地址:北京市东城区青龙胡同甲1号、3号2幢2层205-32室
      • 用户协议
      • 隐私政策
      • 个人信息保护
      • 法律声明
      备案 京公网安备11010802043424号 京ICP备 2021034386号