[英]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.