使用Bash脚本发送包含几天内到期的用户账号列表的电子邮件

系统 Linux
本教程中包含两个 bash 脚本可以帮助你收集系统中用户到期天数的信息。

[[312938]]

密码强制策略对所有操作系统和应用程序都是通用的。如果要在 Linux 上实现密码强制策略,请参阅以下文章。

默认情况下,大多数公司都会强制执行密码强制策略,但根据公司的要求,密码的时间周期会有所不同。通常每个人都使用 90 天的密码周期。用户只会在他们使用的一些服务器上更改密码,而不会在他们不经常使用的服务器上更改密码。

特别地,大多数团队忘记更改服务帐户密码,这可能导致日常工作的中断,即使他们配置有基于 SSH 密钥的身份验证。如果用户帐户密码过期,基于SSH密钥的身份验证和 cronjob 将不起作用。

为了避免这种情况,我们创建了一个 shell 脚本来向你发送 10 天内到期的用户帐户列表。

本教程中包含两个 bash 脚本可以帮助你收集系统中用户到期天数的信息。

1) 检查 10 天后到期的用户帐户列表

此脚本将帮助你在终端上检查 10 天内到期的用户帐户列表。

  1. # vi /opt/script/user-password-expiry.sh
  1. #!/bin/sh
  2. /tmp/user-expiry-1.txt
  3. /tmp/user-expiry.txt
  4. echo "-------------------------------------------------"
  5. echo "UserName The number of days the password expires"
  6. echo "-------------------------------------------------"
  7. for usern in u1 u2 u3 u4
  8. do
  9. today=$(date +%s)
  10. userexpdate=$(chage -l $usern | grep 'Password expires' |cut -d: -f2)
  11. passexp=$(date -d "$userexpdate" "+%s")
  12. exp=`expr \( $passexp - $today \)`
  13. expday=`expr \( $exp / 86400 \)`
  14. echo "$usern $expday" >> /tmp/user-expiry.txt
  15. done
  16. cat /tmp/user-expiry.txt | awk '$2 <= 10' > /tmp/user-expiry-1.txt
  17. cat /tmp/user-expiry-1.txt | column -t

将文件 user-password-expiry.sh 设置为可执行的 Linux 文件权限。

  1. # chmod +x /opt/script/user-password-expiry.sh

你将得到如下输出,但用户与天数可能不同。

  1. # sh /opt/script/user-password-expiry.sh
  2.  
  3. -------------------------------------------------
  4. UserName The number of days the password expires
  5. -------------------------------------------------
  6. u1 -25
  7. u2 9
  8. u3 3
  9. u4 5

2) 发送包含 10 天内到期的用户帐户列表的电子邮件

此脚本将发送一封包含 10 天内到期的用户帐户列表的邮件。

  1. # vi /opt/script/user-password-expiry-mail.sh
  1. #!/bin/sh
  2. SUBJECT="Information About User Password Expiration on "`date`""
  3. MESSAGE="/tmp/user-expiry.txt"
  4. MESSAGE1="/tmp/user-expiry-1.txt"
  5. TO="magesh.m@rentacenter.com"
  6. echo "-------------------------------------------------" >> $MESSAGE1
  7. echo "UserName The number of days the password expires" >> $MESSAGE1
  8. echo "-------------------------------------------------" >> $MESSAGE1
  9. for usern in u1 u2 u3 u4
  10. do
  11. today=$(date +%s)
  12. userexpdate=$(chage -l $usern | grep 'Password expires' |cut -d: -f2)
  13. passexp=$(date -d "$userexpdate" "+%s")
  14. exp=`expr \( $passexp - $today \)`
  15. expday=`expr \( $exp / 86400 \)`
  16. echo "$usern $expday" >> $MESSAGE
  17. done
  18. cat $MESSAGE | awk '$2 <= 10' >> $MESSAGE1
  19. mail -s "$SUBJECT" "$TO" < $MESSAGE1
  20. rm $MESSAGE
  21. rm $MESSAGE1

将文件 user-password-expiry-mail.sh 设置为可执行的 Linux 文件权限。

  1. # chmod +x /opt/script/user-password-expiry-mail.sh

最后,添加一个 cronjob 去自动执行脚本。每天早上 8 点运行一次。

  1. # crontab -e
  2. 0 8 * * * /bin/bash /opt/script/user-password-expiry-mail.sh

你将收到一封与第一个脚本输出类似的电子邮件。 

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

2019-09-20 13:48:23

BashLinux命令

2011-10-31 09:35:50

2019-08-08 07:25:11

BashLinux命令

2020-02-26 13:47:57

Emacs电子邮件开源

2009-10-14 10:10:05

2023-12-31 16:29:31

GoGoroutinesChannels

2010-09-15 14:14:50

2010-09-09 17:11:32

2011-12-15 10:45:33

2020-05-13 08:48:16

JavaScript前端技术

2011-08-01 10:54:56

2011-08-01 11:11:55

2022-03-06 07:01:15

黑客网络攻击

2016-04-28 13:40:04

2020-05-21 10:06:04

电子邮件邮件安全恶意软件

2019-08-08 14:55:19

电子邮件微软信头

2021-06-28 21:21:54

电子邮件邮件安全恶意软件

2011-08-01 12:43:03

2010-06-10 14:10:58

安全电子邮件协议

2023-03-01 09:48:45

点赞
收藏

51CTO技术栈公众号