Current time: 07-04-2022, 10:22 PM Hello There, Guest! (LoginRegister)


Post Reply 
[HowTo] Receive an eMail during a SSH login
Author Message
BeNe Offline
Moderator
*****
Moderators

Posts: 5,899
Joined: Jan 2007
Reputation: 68
Post: #1
[HowTo] Receive an eMail during a SSH login
This is only a one line code, but sometimes very helpful and interesting.
You will recieve an eMail from the Server in seconds if someone login via SSH.
A not invited guest has no more chance Wink

Just add this line under Debian to your "/etc/bash.bashrc"

Code:
# Generate e-Mail on Login
echo 'Login on' `hostname` `date` `who`| mail -s "Login on `hostname` who | awk '{print $5}'`" YOUR@E-MAILADRESS.TLD
Please put all the code (from the echo to your eMailadress) in one line!

I´m sure this works also on other Linux distributions if you know the correct systemwide "bashrc"

Greez BeNe
06-21-2010 05:41 PM
Visit this user's website Find all posts by this user Quote this message in a reply
c0urier Offline
Junior Member
*

Posts: 89
Joined: Jun 2007
Reputation: 1
Post: #2
RE: [HowTo] Receive an eMail during a SSH login
Nice one BeNe, never thought about that tbh =) Thumbs up.
06-22-2010 02:15 AM
Visit this user's website Find all posts by this user Quote this message in a reply
c0urier Offline
Junior Member
*

Posts: 89
Joined: Jun 2007
Reputation: 1
Post: #3
RE: [HowTo] Receive an eMail during a SSH login
@ BeNe,

I think you've added a ` to much.

Code:
# Generate e-Mail on Login
echo 'Login on' `hostname` `date` `who`| mail -s "Login on `hostname` who | awk '{print $5}'`" YOUR@E-MAILADRESS.TLD

Shouldn't it be:

Code:
# Generate e-Mail on Login
echo 'Login on' `hostname` `date` `who`| mail -s "Login on `hostname` who | awk '{print $5}'" YOUR@E-MAILADRESS.TLD
06-29-2010 10:04 AM
Visit this user's website Find all posts by this user Quote this message in a reply
schnere Offline
Junior Member
*

Posts: 40
Joined: Apr 2007
Reputation: 0
Post: #4
RE: [HowTo] Receive an eMail during a SSH login
@c0urier: It should be
Code:
echo 'Login on' `hostname` `date` `who`| mail -s "Login on `hostname` `who | awk '{print $5}'`" YOUR@E-MAILADRESS.TLD

You may add it to /etc/profile because /etc/bash.bashrc only gets executed on starting a root shell, but /etc/profile gets executed on every user login.
(This post was last modified: 08-04-2010 07:00 PM by schnere.)
08-04-2010 06:51 PM
Visit this user's website Find all posts by this user Quote this message in a reply
c0urier Offline
Junior Member
*

Posts: 89
Joined: Jun 2007
Reputation: 1
Post: #5
RE: [HowTo] Receive an eMail during a SSH login
(08-04-2010 06:51 PM)schnere Wrote:  @c0urier: It should be
Code:
echo 'Login on' `hostname` `date` `who`| mail -s "Login on `hostname` `who | awk '{print $5}'`" YOUR@E-MAILADRESS.TLD

You may add it to /etc/profile because /etc/bash.bashrc only gets executed on starting a root shell, but /etc/profile gets executed on every user login.

Well the way I posted it, it works in my /etc/bash.bashrc - But only when I removed the extra signs =).
08-04-2010 08:25 PM
Visit this user's website Find all posts by this user Quote this message in a reply
schnere Offline
Junior Member
*

Posts: 40
Joined: Apr 2007
Reputation: 0
Post: #6
RE: [HowTo] Receive an eMail during a SSH login
(08-04-2010 08:25 PM)c0urier Wrote:  Well the way I posted it, it works in my /etc/bash.bashrc - But only when I removed the extra signs =).

Yes, it works, but not how it should work, because who | awk '{print $5}' is a shell command which only gets executed within ` signs.
With your variant you will receive emails with "who | awk '{print }'" in subject instead of the ip address.
And yes, it works in /etc/bash.bashrc , but for me in Debian Lenny /etc/bash.bashrc only gets executed on root login. Because I want to see every user login, my solution is to use /etc/profile . Just wanted to share my thoughts.

Regards, schnere
(This post was last modified: 08-04-2010 09:17 PM by schnere.)
08-04-2010 09:16 PM
Visit this user's website Find all posts by this user Quote this message in a reply
c0urier Offline
Junior Member
*

Posts: 89
Joined: Jun 2007
Reputation: 1
Post: #7
RE: [HowTo] Receive an eMail during a SSH login
@schnere

If I don't remove the extra ` that is added in BeNe's example I get errors. I'm sure that extra ` should not be there.

This works for me:
WRONG:
Code:
echo 'Login on' `hostname` `date` `who`| mail -s "Login on `hostname` who | awk '{print $5}'" YOUR@E-MAILADRESS.TLD

And not:
WRONG:
Code:
echo 'Login on' `hostname` `date` `who`| mail -s "Login on `hostname` `who | awk '{print $5}'`" YOUR@E-MAILADRESS.TLD

I've added it in the profiles and correct you receive a mail at every login now, but not with the second example it just generates an error.
Tested on Debian Lenny, Debian Squeeze and CentOS 5.0.

And you are right, it has a better effect in /etc/profile - Thanks for that!

*****************
@ schnere Nr2: =)
I can see you added an extra ` which is not added in BeNe's suggestion, so the correct is as you write.
Correct:
Code:
echo 'Login on' `hostname` `date` `who`| mail -s "Login on `hostname` `who | awk '{print $5}'`" YOUR@E-MAILADRESS.TLD
(This post was last modified: 08-05-2010 01:28 AM by c0urier.)
08-05-2010 12:40 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Knut Offline
Member
***

Posts: 736
Joined: Nov 2006
Reputation: 10
Post: #8
RE: [HowTo] Receive an eMail during a SSH login
[Image: gm.jpg]

Thank you BeNe
08-05-2010 04:59 AM
Find all posts by this user Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 1 Guest(s)