用Bash脚本发送新用户帐户创建的邮件
出于某些原因,你可能需要跟踪 Linux 上的新用户创建信息。同时,你可能需要通过邮件发送详细信息。这或许是审计目标的一部分,或者安全团队出于跟踪目的可能希望对此进行监控。
我们可以通过其他方式进行此操作,正如我们在上一篇文章中已经描述的那样。
Linux 有许多开源监控工具可以使用。但我不认为他们有办法跟踪新用户创建过程,并在发生时提醒管理员。
那么我们怎样才能做到这一点?
我们可以编写自己的 Bash 脚本来实现这一目标。我们过去写过许多有用的 shell 脚本。如果你想了解,请进入下面的链接。
这个脚本做了什么?
这将每天两次(一天的开始和结束)备份 /etc/passwd
文件,这将使你能够获取指定日期的新用户创建详细信息。
我们需要添加以下两个 cron 任务来复制 /etc/passwd
文件。
<span class="com">#</span><span class="pln"> crontab </span><span class="pun">-</span><span class="pln">e</span>
<span class="lit">1</span><span class="pln"> </span><span class="lit">0</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="kwd">cp</span><span class="pln"> </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="kwd">passwd</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">passwd</span><span class="pun">-</span><span class="pln">start</span><span class="pun">-</span><span class="pln">$</span><span class="pun">(</span><span class="kwd">date</span><span class="pln"> </span><span class="pun">+</span><span class="str">"%Y-%m-%d"</span><span class="pun">)</span>
<span class="lit">59</span><span class="pln"> </span><span class="lit">23</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="kwd">cp</span><span class="pln"> </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="kwd">passwd</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">passwd</span><span class="pun">-</span><span class="kwd">end</span><span class="pun">-</span><span class="pln">$</span><span class="pun">(</span><span class="kwd">date</span><span class="pln"> </span><span class="pun">+</span><span class="str">"%Y-%m-%d"</span><span class="pun">)</span>
它使用 diff
命令来检测文件之间的差异,如果发现与昨日有任何差异,脚本将向指定 email 发送新用户详细信息。
我们不用经常运行此脚本,因为用户创建不经常发生。但是,我们计划每天运行一次此脚本。
这样,你可以获得有关新用户创建的综合报告。
注意:我们在脚本中使用了我们的电子邮件地址进行演示。因此,我们要求你用自己的电子邮件地址。
<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">detail</span><span class="pun">.</span><span class="pln">sh</span>
<span class="com">#</span><span class="pun">!</span><span class="str">/bin/</span><span class="kwd">bash</span>
<span class="kwd">mv</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">passwd</span><span class="pun">-</span><span class="pln">start</span><span class="pun">-</span><span class="pln">$</span><span class="pun">(</span><span class="kwd">date</span><span class="pln"> </span><span class="pun">--</span><span class="kwd">date</span><span class="pun">=</span><span class="str">'yesterday'</span><span class="pln"> </span><span class="str">'+%Y-%m-%d'</span><span class="pun">)</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">passwd</span><span class="pun">-</span><span class="pln">start</span>
<span class="kwd">mv</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">passwd</span><span class="pun">-</span><span class="kwd">end</span><span class="pun">-</span><span class="pln">$</span><span class="pun">(</span><span class="kwd">date</span><span class="pln"> </span><span class="pun">--</span><span class="kwd">date</span><span class="pun">=</span><span class="str">'yesterday'</span><span class="pln"> </span><span class="str">'+%Y-%m-%d'</span><span class="pun">)</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">passwd</span><span class="pun">-</span><span class="kwd">end</span>
<span class="pln">ucount</span><span class="pun">=</span><span class="pln">$</span><span class="pun">(</span><span class="kwd">diff</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">passwd</span><span class="pun">-</span><span class="pln">start </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">passwd</span><span class="pun">-</span><span class="kwd">end</span><span class="pln"> </span><span class="pun">|</span><span class="pln"> </span><span class="kwd">grep</span><span class="pln"> </span><span class="str">">"</span><span class="pln"> </span><span class="pun">|</span><span class="pln"> </span><span class="kwd">cut</span><span class="pln"> </span><span class="pun">-</span><span class="pln">d</span><span class="str">":"</span><span class="pln"> </span><span class="pun">-</span><span class="pln">f6 </span><span class="pun">|</span><span class="pln"> </span><span class="kwd">cut</span><span class="pln"> </span><span class="pun">-</span><span class="pln">d</span><span class="str">"/"</span><span class="pln"> </span><span class="pun">-</span><span class="pln">f3 </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>
<span class="kwd">if</span><span class="pln"> </span><span class="pun">[</span><span class="pln"> $ucount </span><span class="pun">-</span><span class="pln">gt </span><span class="lit">0</span><span class="pln"> </span><span class="pun">]</span>
<span class="kwd">then</span>
<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>
<span class="pln"> MESSAGE</span><span class="pun">=</span><span class="str">"/tmp/new-user-logs.txt"</span>
<span class="pln"> TO</span><span class="pun">=</span><span class="str">"[email protected]"</span>
<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>
<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>
<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>
<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>
<span class="pln"> </span><span class="kwd">diff</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">passwd</span><span class="pun">-</span><span class="pln">start </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">passwd</span><span class="pun">-</span><span class="kwd">end</span><span class="pln"> </span><span class="pun">|</span><span class="pln"> </span><span class="kwd">grep</span><span class="pln"> </span><span class="str">">"</span><span class="pln"> </span><span class="pun">|</span><span class="pln"> </span><span class="kwd">cut</span><span class="pln"> </span><span class="pun">-</span><span class="pln">d</span><span class="str">":"</span><span class="pln"> </span><span class="pun">-</span><span class="pln">f6 </span><span class="pun">|</span><span class="pln"> </span><span class="kwd">cut</span><span class="pln"> </span><span class="pun">-</span><span class="pln">d</span><span class="str">"/"</span><span class="pln"> </span><span class="pun">-</span><span class="pln">f3 </span><span class="pun">>></span><span class="pln"> $MESSAGE</span>
<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>
<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>
<span class="pln"> </span><span class="kwd">rm</span><span class="pln"> $MESSAGE</span>
<span class="kwd">fi</span><span class="pln"> </span>
给 new-user-detail.sh
文件添加可执行权限。
<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">detail</span><span class="pun">.</span><span class="pln">sh</span>
最后添加一个 cron 任务来自动执行此操作。它在每天早上 7 点运行。
<span class="com">#</span><span class="pln"> crontab </span><span class="pun">-</span><span class="pln">e</span>
<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 点都会收到一封关于昨日详情的邮件提醒。
相关推荐
touchfuture 2020-09-16
fsfsdfsdw 2020-04-22
applecarelte 2020-10-16
Testingba工作室 2020-09-15
TuxedoLinux 2020-09-11
guying 2020-08-16
haitianisgood 2020-07-21
luobotoutou 2020-07-20
luvzhan 2020-06-28
Jieen 2020-06-28
renshuguo 2020-06-24
cutdeer 2020-06-17
lishaokang 2020-06-08
DBATips 2020-06-07
xiaobater 2020-05-26
木子李CSDN 2020-05-18
死鱼喜欢猫 2020-05-15