在CentOS/RHEL系统上生成补丁合规报告的Bash脚本

系统 Linux 系统运维
如果你运行的是大型 Linux 环境,那么你可能已经将 Red Hat 与 Satellite 集成了。如果是的话,你不必担心补丁合规性报告,因为有一种方法可以从 Satellite 服务器导出它。

[[282688]]

如果你运行的是大型 Linux 环境,那么你可能已经将 Red Hat 与 Satellite 集成了。如果是的话,你不必担心补丁合规性报告,因为有一种方法可以从 Satellite 服务器导出它。

但是,如果你运行的是没有 Satellite 集成的小型 Red Hat 环境,或者它是 CentOS 系统,那么此脚本将帮助你创建该报告。

补丁合规性报告通常每月创建一次或三个月一次,具体取决于公司的需求。根据你的需要添加 cronjob 来自动执行此功能。

bash 脚本 通常适合于少于 50 个系统运行,但没有限制。

保持系统最新是 Linux 管理员的一项重要任务,它使你的计算机非常稳定和安全。

此教程中包含四个 shell 脚本,请选择适合你的脚本。

方法 1:为 CentOS / RHEL 系统上的安全修补生成补丁合规性报告的 Bash 脚本

此脚本只会生成安全修补合规性报告。它会通过纯文本发送邮件。

  1. # vi /opt/scripts/small-scripts/sec-errata.sh
  2.  
  3. #!/bin/sh
  4. /tmp/sec-up.txt
  5. SUBJECT="Patching Reports on "date""
  6. MESSAGE="/tmp/sec-up.txt"
  7. TO="[email protected]"
  8. echo "+---------------+-----------------------------+" >> $MESSAGE
  9. echo "| Server_Name | Security Errata |" >> $MESSAGE
  10. echo "+---------------+-----------------------------+" >> $MESSAGE
  11. for server in `more /opt/scripts/server.txt`
  12. do
  13. sec=`ssh $server yum updateinfo summary | grep 'Security' | grep -v 'Important|Moderate' | tail -1 | awk '{print $1}'`
  14. echo "$server $sec" >> $MESSAGE
  15. done
  16. echo "+---------------------------------------------+" >> $MESSAGE
  17. mail -s "$SUBJECT" "$TO" < $MESSAGE

添加完上面的脚本后运行它。

  1. # sh /opt/scripts/small-scripts/sec-errata.sh

你会看到下面的输出。

  1. # cat /tmp/sec-up.txt
  2.  
  3. +---------------+-------------------+
  4. | Server_Name | Security Errata |
  5. +---------------+-------------------+
  6. server1
  7. server2
  8. server3 21
  9. server4
  10. +-----------------------------------+

添加下面的 cronjob 来每个月得到一份补丁合规性报告。

  1. # crontab -e
  2.  
  3. @monthly /bin/bash /opt/scripts/system-uptime-script-1.sh

方法 1a:为 CentOS / RHEL 系统上的安全修补生成补丁合规性报告的 Bash 脚本

脚本会为你生成安全修补合规性报告。它会通过 CSV 文件发送邮件。

  1. # vi /opt/scripts/small-scripts/sec-errata-1.sh
  2.  
  3. #!/bin/sh
  4. echo "Server Name, Security Errata" > /tmp/sec-up.csv
  5. for server in `more /opt/scripts/server.txt`
  6. do
  7. sec=`ssh $server yum updateinfo summary | grep 'Security' | grep -v 'Important|Moderate' | tail -1 | awk '{print $1}'`
  8. echo "$server, $sec" >> /tmp/sec-up.csv
  9. done
  10. echo "Patching Report for `date +"%B %Y"`" | mailx -s "Patching Report on `date`" -a /tmp/sec-up.csv [email protected]
  11. rm /tmp/sec-up.csv

添加完上面的脚本后运行它。

  1. # sh /opt/scripts/small-scripts/sec-errata-1.sh

你会看到下面的输出。

 

方法 2:为 CentOS / RHEL 系统上的安全修补、bugfix、增强生成补丁合规性报告的 Bash 脚本

脚本会为你生成安全修补、bugfix、增强的补丁合规性报告。它会通过纯文本发送邮件。

  1. # vi /opt/scripts/small-scripts/sec-errata-bugfix-enhancement.sh
  2.  
  3. #!/bin/sh
  4. /tmp/sec-up.txt
  5. SUBJECT="Patching Reports on "`date`""
  6. MESSAGE="/tmp/sec-up.txt"
  7. TO="[email protected]"
  8. echo "+---------------+-------------------+--------+---------------------+" >> $MESSAGE
  9. echo "| Server_Name | Security Errata | Bugfix | Enhancement |" >> $MESSAGE
  10. echo "+---------------+-------------------+--------+---------------------+" >> $MESSAGE
  11. for server in `more /opt/scripts/server.txt`
  12. do
  13. sec=`ssh $server yum updateinfo summary | grep 'Security' | grep -v 'Important|Moderate' | tail -1 | awk '{print $1}'`
  14. bug=`ssh $server yum updateinfo summary | grep 'Bugfix' | tail -1 | awk '{print $1}'`
  15. enhance=`ssh $server yum updateinfo summary | grep 'Enhancement' | tail -1 | awk '{print $1}'`
  16. echo "$server $sec $bug $enhance" >> $MESSAGE
  17. done
  18. echo "+------------------------------------------------------------------+" >> $MESSAGE
  19. mail -s "$SUBJECT" "$TO" < $MESSAGE

添加完上面的脚本后运行它。

  1. # sh /opt/scripts/small-scripts/sec-errata-bugfix-enhancement.sh

你会看到下面的输出。

  1. # cat /tmp/sec-up.txt
  2.  
  3. +---------------+-------------------+--------+---------------------+
  4. | Server_Name | Security Errata | Bugfix | Enhancement |
  5. +---------------+-------------------+--------+---------------------+
  6. server01 16
  7. server02 5 16
  8. server03 21 266 20
  9. server04 16
  10. +------------------------------------------------------------------+

添加下面的 cronjob 来每三个月得到补丁合规性报告。该脚本计划在一月、四月、七月、十月的 1 号运行。

  1. # crontab -e
  2.  
  3. 0 0 01 */3 * /bin/bash /opt/scripts/system-uptime-script-1.sh

方法 2a:为 CentOS / RHEL 系统上的安全修补、bugfix、增强生成补丁合规性报告的 Bash 脚本

脚本会为你生成安全修补、bugfix、增强的补丁合规性报告。它会通过 CSV 文件发送邮件。

  1. # vi /opt/scripts/small-scripts/sec-errata-bugfix-enhancement-1.sh
  2.  
  3. #!/bin/sh
  4. echo "Server Name, Security Errata,Bugfix,Enhancement" > /tmp/sec-up.csv
  5. for server in `more /opt/scripts/server.txt`
  6. do
  7. sec=`ssh $server yum updateinfo summary | grep 'Security' | grep -v 'Important|Moderate' | tail -1 | awk '{print $1}'`
  8. bug=`ssh $server yum updateinfo summary | grep 'Bugfix' | tail -1 | awk '{print $1}'`
  9. enhance=`ssh $server yum updateinfo summary | grep 'Enhancement' | tail -1 | awk '{print $1}'`
  10. echo "$server,$sec,$bug,$enhance" >> /tmp/sec-up.csv
  11. done
  12. echo "Patching Report for `date +"%B %Y"`" | mailx -s "Patching Report on `date`" -a /tmp/sec-up.csv [email protected]
  13. rm /tmp/sec-up.csv

添加完上面的脚本后运行它。

  1. # sh /opt/scripts/small-scripts/sec-errata-bugfix-enhancement-1.sh

你会看到下面的输出。

 

责任编辑:庞桂玉 来源: Linux中国
相关推荐

2019-10-14 09:14:37

Linuxbash命令

2019-08-01 09:35:09

LinuxBashmessages

2016-12-07 18:12:05

CentOSRHEL安全补丁

2018-05-31 21:53:17

云合规云计算多云

2018-01-18 09:34:27

LinuxCentOSYUM

2018-01-09 09:20:39

CentOSRHELyum

2013-10-17 10:24:01

IT合规性合规性法规遵从

2014-09-05 10:15:20

CentOSRHEL 7

2019-06-05 10:20:09

安全更新命令Linux

2019-08-08 07:25:11

BashLinux命令

2012-05-23 09:32:53

身份管理IAM系统

2020-10-13 19:04:58

Bash信号捕获Shell脚本

2016-11-08 08:51:43

GitLinux开源

2014-07-24 10:17:25

CentOSSquid

2023-08-28 16:01:17

LinuxRHEL网络绑定

2019-11-11 15:10:37

FedoraLinuxbash

2023-09-15 16:30:48

2010-11-30 17:42:10

2014-09-04 09:18:15

2021-07-12 14:23:17

物联网人工智能IoT
点赞
收藏

51CTO技术栈公众号