简体   繁体   English

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

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

I am using the below command in my .bash_profile to get email alerts on ssh user logins 我在.bash_profile中使用以下命令来获取有关ssh用户登录的电子邮件警报

I get the email alerts, the only problem is format of whois command output - its wrapped. 我收到电子邮件警报,唯一的问题是whois命令输出的格式-它的包装形式。

Output of whois in command line is neat. 在命令行中whois输出是整齐的。 Even after using sed -r G it doesn't work. 即使在使用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

SOLUTION: 解:

Paste the following in .bash_profile in the user/root directory to get email alerts 将以下内容粘贴到用户/根目录中的.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

For whois to work on RHEL based systems like CentOS you would need to install jwhois 为了使whois在基于RHEL的系统(例如CentOS)上工作,您需要安装jwhois

You need to double-quote the echo statement in its entirety: 您需要将echo语句完整地双引号:

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

That will prevent wrapping of the whois output before it is passed to sed -- preserving all the linebreaks. 这将防止在将whois输出传递给sed之前包装其whois输出-保留所有换行符。 The remainder of your mailx line should be fine. 您的mailx行的其余部分应该没问题。 One other thought. 另一个想法。 Why not just write all of the output to a tmp file following | 为什么不将所有输出都写入下面的tmp文件中? sed -r G > tmpfile. sed -r G> tmpfile。 Then just add the tmpfile as an attachment to mailx? 然后,只需将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