我需要通过电子邮件通知我的Ubuntu服务器通过SSH访问。
如果可能,我该怎么办?
当用户login时, pam_script将运行你想要的任何程序。
您应该可以使用/etc/hosts.allow的规则来执行此操作。 尝试像这样:
sshd: ALL: (/usr/bin/echo "SSH connection from %h (%H)" | /usr/bin/mailx -s "SSH Alert" [email protected])
您可以从/etc/profile.d运行的脚本获取更多详细信息,或者包含在/etc/profile 。 但是,这只有在用户login到交互式会话时才有效。
如果您不需要即时通知,则logcheck程序可以每小时通知您上一小时的任何访问。 您将需要添加适当的规则到configuration。
编辑:Ubuntu使用不兼容hosts_options格式来执行shell命令。 遵循的规则是我执行的:
SSHD: ALL: spawn (/bin/echo "SSH connection to %H from %h[%a]" | \ /usr/bin/mailx -s "SSH Alert" [email protected])
备注:可以使用反斜杠符号来包装上面的行。 replace字符logging在hosts.allow手册页中。
最好的办法是设置一个脚本来观看日志文件。
我现在正在打电话,但是检查/var/log/access.log
日志是在auth.log上,你可以做一个
cat /var/log/auth.log | grep ssh
要真正发送邮件,你可以安装SSMTP,编辑它的configuration如下:
/etc/ssmtp/ssmtp.conf
root = [email protected] mailhub = smtp.gmail.com:465 rewriteDomain = gmail.com AuthUser = yourusername AuthPass = yourpassword FromLineOverride = YES UseTLS = YES
使用消息正文创build一个文本文件,如下所示:
发件人:[email protected]发件人:[email protected]主题:SSH警告或其他
邮件内容
要添加您可以执行的内容:
tail /var/log/auth.log | grep ssh >> /tmp/mailcontents.txt
然后运行
ssmtp [email protected] < /tmp/mailcontents.txt
编辑:
另一个OP说你每次发生这种情况都需要通知,你可以这样做:
创build一个脚本
!/bin/sh tail /var/log/auth.log | grep ssh >> /tmp/alert& while true; do change=$(inotifywait -e close_write,moved_to,create .) change=${change#./ * } if [ "$change" = "/tmp/alert" ]; then tail -n 1 /tmp/alert >> /tmp/mailcontents.txt ssmtp [email protected] < mailcontents.txt; fi done
邮件内容应该包括前面说过的地址,脚本代码没有被检查是有效的,认为是伪代码。