繁体   English   中英

使用mailx登录有关用户的电子邮件whois详细信息-使用sed的whois输出格式不起作用

[英]Email whois details on user login using mailx - whois output format not working using sed

我在.bash_profile中使用以下命令来获取有关ssh用户登录的电子邮件警报

我收到电子邮件警报,唯一的问题是whois命令输出的格式-它的包装形式。

在命令行中whois输出是整齐的。 即使在使用sed -r G它也不起作用。

echo -e 'ALERT - Shell Access on:' `date` `who` '\n\n' `whois $(who | cut -d'(' -f2 | cut -d')' -f1)` | sed -r G | mail -s "Alert: SSH Access from `who | cut -d'(' -f2 | cut -d')' -f1`" user@example.com

解:

将以下内容粘贴到用户/根目录中的.bash_profile中,以获取电子邮件警报

# Send email on user login - manually added
SSHLOGINIP=`who | cut -d'(' -f2 | cut -d')' -f1 | tail -n 1`
echo -e "ALERT - Shell Access on: `date` \n\n Active Sessions:\n `who` \n\n `whois $SSHLOGINIP`" | sed -r G | mail -s "Alert: SSH Access from $SSHLOGINIP" user@example.com
unset SSHLOGINIP

为了使whois在基于RHEL的系统(例如CentOS)上工作,您需要安装jwhois

您需要将echo语句完整地双引号:

echo -e "ALERT - Shell Access on:' `date` `who` '\n\n' `whois $(who | cut -d'(' -f2 | cut -d')' -f1)`"

这将防止在将whois输出传递给sed之前包装其whois输出-保留所有换行符。 您的mailx行的其余部分应该没问题。 另一个想法。 为什么不将所有输出都写入下面的tmp文件中? sed -r G> tmpfile。 然后,只需将tmpfile添加为mailx的附件?

mail -s "Alert: SSH Access from `who | cut -d'(' -f2 | cut -d')' -f1`" -a tmpfile user@example.com

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM