close

Linux Server Login Alert Script

The following script demonstates a way to send out an alert email when someone logs in the Linux Server.

vi /script/login_alert.sh


#!/bin/bash
host=`hostname`
user=`whoami`
ip="$(echo $SSH_CLIENT|awk '{print $1}')"
mkdir -p ~/ServerLoginTrace
echo 'ALERT - ' $user ' IP:'$ip ' Shell Access' $host > ~/ServerLoginTrace/alert_message.txt
pinky >> ~/ServerLoginTrace/alert_message.txt
chmod 600 ~/ServerLoginTrace/alert_message.txt
mutt -s "Login Monitor from $host" -- danbrother@pixnet.net < ~/ServerLoginTrace/alert_message.txt

 

echo ". /script/login_alert.sh > /dev/null 2>&1 &" > /etc/ssh/sshrc
chown root:dba /etc/ssh/sshrc
chmod 770 /etc/ssh/sshrc

 

$ cat ~/ServerLoginTrace/alert_message.txt
ALERT - danbrother  IP:168.95.1.101  Shell Access   MyTestServer
Login Name   TTY     Idle  When              Where
danbrother    pts/0           Mar 5 10:22   168.95.1.101

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 DanBrother 的頭像
    DanBrother

    DanBrother的部落格

    DanBrother 發表在 痞客邦 留言(0) 人氣()