terça-feira, fevereiro 16, 2016

Set up an email alert when a ssh login is successful

cp /root/sendEmail /usr/local/sbin/
chmod +x /usr/local/sbin/sendEmail

vim /etc/ssh/login-notify.sh

#!/bin/sh

# Change these two lines:
EMAIL_FROM=root@`hostname`
EMATIL_TO=youemail@domain.com

if [ "$PAM_TYPE" != "close_session" ]; then
    ip=`echo $SSH_CONNECTION | cut -d " " -f 1`
    host="`hostname`"
    subject="SSH Login: $PAM_USER from $PAM_RHOST on $host from $ip"
    # Message to send, e.g. the current environment variables.
     message="`nslookup $PAM_RHOST|grep name` \n `env`"
    /usr/local/sbin/sendEmail -t $EMATIL_TO -f $EMAIL_FROM -u $subject -m $message
#    echo "$message" | mailx -r "$sender" -s "$subject" "$recepient"
fi

##END FILE


chmod +x /etc/ssh/login-notify.sh

vim /etc/pam.d/sshd

#add this lines
session optional pam_exec.so seteuid /etc/ssh/login-notify.sh

Nenhum comentário: