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

1 Answer 1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .