在ssh登录时发送电子邮件

时间:2011-09-13 16:40:33

标签: bash email ssh

我想确保每当有人登录我的服务器时,我会收到一封电子邮件说:

  

ALERT Shell Access on:Tue Jun 16 11:04:10 CDT 2009 user123 pts / 0   2009-06-16 11:04

所以我把这段代码:

echo 'ALERT Shell Access on:' `date` `who` | mail -s "Alert: Root Access from `who | cut -d"(" -f2 | cut -d")" -f1`" example@example.com
<{1>}中的

为什么我没有收到任何电子邮件?

感谢您的帮助,J

2 个答案:

答案 0 :(得分:11)

.bashrc是在bash作为交互式非登录shell运行时执行的,而ssh则不是这种情况。 在.bash_profile中添加相同的代码,该代码应该在shell是登录shell时运行

注意:许多发行版从.bash_profile中获取.bashrc,我可能错了,问题可能不是关于.bashrc / .bash_profile

答案 1 :(得分:0)

加入/etc/ssh/sshrc

ip=`echo $SSH_CONNECTION | cut -d " " -f 1`

logger -t ssh-wrapper $USER login from $ip
msg="User $USER just logged in from $ip"
echo $msg|mail -s $msg root