CentOS bashrc function to email on login

0

I've a system setup where an email is sent on a user login (line from .bashrc):

printf "user details, ip etc" | mail -s "[LOGIN NOTICE] `hostname` - `whoami`" <admin>@<domain>.co.uk

This works, I'm looking to make it a little smarter. it will email on any login, even on a SCP transfer. Can anyone suggest how I can detect and exclude SCP or TTY etc.

Thanks in advance

fir3x

Posted 2015-01-20T10:37:22.737

Reputation: 11

Answers

0

.bashrc is sourced whenever a new shell is started – regardless of whether the shell is interactive, a sub-shell of an existing Bash shell, etc. I presume what you want is to check whether or not the login shell is interactive or not:

if [[ $- == *i* ]]; then
    printf "user details, ip etc" | mail -s "[LOGIN NOTICE] `hostname` - `whoami`" <admin>@<domain>.co.uk
fi

A portable (non-Bash specific) way to check if the shell being started is interactive or not would be:

case "$-" in
    *i*)    printf "user details, ip etc" | mail -s "[LOGIN NOTICE] `hostname` - `whoami`" <admin>@<domain>.co.uk
esac

See GNU Bash manual.

This Unix and Linux Stack Exchange question also has some relevant answers.

Anthony Geoghegan

Posted 2015-01-20T10:37:22.737

Reputation: 3 095