在系统创建新用户时发送邮件的Bash脚本

在系统创建新用户时发送邮件的Bash脚本

目前市场上有许多开源监测工具可用于监控 Linux 系统的性能。当系统到达指定的阈值时,它将发送邮件提醒。

它会监控 CPU 利用率、内存利用率、交换内存利用率、磁盘空间利用率等所有内容。但我不认为它们可以选择监控新用户创建活动,并发送提醒。

如果没有,这并不重要,因为我们可以编写自己的 bash 脚本来实现这一点。

我们过去写了许多有用的 shell 脚本。如果要查看它们,请点击以下链接。

这个脚本做了什么?它监测 /var/log/secure 文件,并在系统创建新帐户时提醒管理员。

我们不会经常运行此脚本,因为创建用户不经常发生。但是,我打算一天运行一次这个脚本。因此,我们可以获得有关用户创建的综合报告。

如果在昨天的 /var/log/secure 中找到了 “useradd” 字符串,那么该脚本将向指定的邮箱发送邮件提醒,其中包含了新用户的详细信息。

注意:你需要更改邮箱而不是使用我们的邮箱。

  1. <span class="com">#</span><span class="pln"> </span><span class="kwd">vi</span><span class="pln"> </span><span class="pun">/</span><span class="pln">opt</span><span class="pun">/</span><span class="pln">scripts</span><span class="pun">/</span><span class="kwd">new</span><span class="pun">-</span><span class="pln">user</span><span class="pun">.</span><span class="pln">sh</span>
  1. <span class="com">#</span><span class="pun">!</span><span class="str">/bin/</span><span class="kwd">bash</span>
  2.  
  3. <span class="com">#</span><span class="typ">Set</span><span class="pln"> the variable which equal to zero</span>
  4. <span class="pln">prev_count</span><span class="pun">=</span><span class="lit">0</span>
  5. <span class="pln">count</span><span class="pun">=</span><span class="pln">$</span><span class="pun">(</span><span class="kwd">grep</span><span class="pln"> </span><span class="pun">-</span><span class="pln">i </span><span class="str">"`date --date='yesterday' '+%b %e'`"</span><span class="pln"> </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">log</span><span class="pun">/</span><span class="pln">secure </span><span class="pun">|</span><span class="pln"> egrep </span><span class="pun">-</span><span class="pln">wi </span><span class="str">'useradd'</span><span class="pln"> </span><span class="pun">|</span><span class="pln"> </span><span class="kwd">wc</span><span class="pln"> </span><span class="pun">-</span><span class="pln">l</span><span class="pun">)</span>
  6.  
  7. <span class="kwd">if</span><span class="pln"> </span><span class="pun">[</span><span class="pln"> </span><span class="str">"$prev_count"</span><span class="pln"> </span><span class="pun">-</span><span class="pln">lt </span><span class="str">"$count"</span><span class="pln"> </span><span class="pun">]</span><span class="pln"> </span><span class="pun">;</span><span class="pln"> </span><span class="kwd">then</span>
  8. <span class="pln"> </span><span class="com">#</span><span class="pln"> </span><span class="typ">Send</span><span class="pln"> a mail to given email </span><span class="kwd">id</span><span class="pln"> </span><span class="kwd">when</span><span class="pln"> errors found </span><span class="kwd">in</span><span class="pln"> log</span>
  9. <span class="pln"> SUBJECT</span><span class="pun">=</span><span class="str">"ATTENTION: New User Account is created on server : `date --date='yesterday' '+%b %e'`"</span>
  10. <span class="pln"> </span><span class="com">#</span><span class="pln"> </span><span class="typ">This</span><span class="pln"> </span><span class="kwd">is</span><span class="pln"> a temp </span><span class="kwd">file</span><span class="pun">,</span><span class="pln"> which </span><span class="kwd">is</span><span class="pln"> created to store the email message</span><span class="pun">.</span>
  11. <span class="pln"> MESSAGE</span><span class="pun">=</span><span class="str">"/tmp/new-user-logs.txt"</span>
  12. <span class="pln"> TO</span><span class="pun">=</span><span class="str">"[email protected]"</span>
  13. <span class="pln"> </span><span class="kwd">echo</span><span class="pln"> </span><span class="str">"Hostname: `hostname`"</span><span class="pln"> </span><span class="pun">>></span><span class="pln"> $MESSAGE</span>
  14. <span class="pln"> </span><span class="kwd">echo</span><span class="pln"> </span><span class="pun">-</span><span class="pln">e </span><span class="str">"\n"</span><span class="pln"> </span><span class="pun">>></span><span class="pln"> $MESSAGE</span>
  15. <span class="pln"> </span><span class="kwd">echo</span><span class="pln"> </span><span class="str">"The New User Details are below."</span><span class="pln"> </span><span class="pun">>></span><span class="pln"> $MESSAGE</span>
  16. <span class="pln"> </span><span class="kwd">echo</span><span class="pln"> </span><span class="str">"+------------------------------+"</span><span class="pln"> </span><span class="pun">>></span><span class="pln"> $MESSAGE</span>
  17. <span class="pln"> </span><span class="kwd">grep</span><span class="pln"> </span><span class="pun">-</span><span class="pln">i </span><span class="str">"`date --date='yesterday' '+%b %e'`"</span><span class="pln"> </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">log</span><span class="pun">/</span><span class="pln">secure </span><span class="pun">|</span><span class="pln"> egrep </span><span class="pun">-</span><span class="pln">wi </span><span class="str">'useradd'</span><span class="pln"> </span><span class="pun">|</span><span class="pln"> </span><span class="kwd">grep</span><span class="pln"> </span><span class="pun">-</span><span class="pln">v </span><span class="str">'failed adding'</span><span class="pun">|</span><span class="pln"> awk </span><span class="str">'{print $4,$8}'</span><span class="pln"> </span><span class="pun">|</span><span class="pln"> </span><span class="kwd">uniq</span><span class="pln"> </span><span class="pun">|</span><span class="pln"> </span><span class="kwd">sed</span><span class="pln"> </span><span class="str">'s/,/ /'</span><span class="pln"> </span><span class="pun">>></span><span class="pln"> $MESSAGE</span>
  18. <span class="pln"> </span><span class="kwd">echo</span><span class="pln"> </span><span class="str">"+------------------------------+"</span><span class="pln"> </span><span class="pun">>></span><span class="pln"> $MESSAGE</span>
  19. <span class="pln"> mail </span><span class="pun">-</span><span class="pln">s </span><span class="str">"$SUBJECT"</span><span class="pln"> </span><span class="str">"$TO"</span><span class="pln"> </span><span class="pun"><</span><span class="pln"> $MESSAGE</span>
  20. <span class="pln"> </span><span class="kwd">rm</span><span class="pln"> $MESSAGE</span>
  21. <span class="kwd">fi</span>

new-user.sh 添加可执行权限。

  1. <span class="pln">$ </span><span class="kwd">chmod</span><span class="pln"> </span><span class="pun">+</span><span class="pln">x </span><span class="pun">/</span><span class="pln">opt</span><span class="pun">/</span><span class="pln">scripts</span><span class="pun">/</span><span class="kwd">new</span><span class="pun">-</span><span class="pln">user</span><span class="pun">.</span><span class="pln">sh</span>

最后添加一个 cron 任务来自动化执行它。它会在每天 7 点运行。

  1. <span class="com">#</span><span class="pln"> crontab </span><span class="pun">-</span><span class="pln">e</span>
  2.  
  3. <span class="lit">0</span><span class="pln"> </span><span class="lit">7</span><span class="pln"> </span><span class="pun">*</span><span class="pln"> </span><span class="pun">*</span><span class="pln"> </span><span class="pun">*</span><span class="pln"> </span><span class="str">/bin/</span><span class="kwd">bash</span><span class="pln"> </span><span class="pun">/</span><span class="pln">opt</span><span class="pun">/</span><span class="pln">scripts</span><span class="pun">/</span><span class="kwd">new</span><span class="pun">-</span><span class="pln">user</span><span class="pun">.</span><span class="pln">sh</span>

注意:你将在每天 7 点收到一封邮件提醒,但这是昨天的日志。

相关推荐