Gentoo Forums
Gentoo Forums
Quick Search: in
Email notification when user logs on via ssh
View unanswered posts
View posts from last 24 hours

rackathon
 
Reply to topic    Gentoo Forums Forum Index Networking & Security
View previous topic :: View next topic  
Author Message
F4lC0N
n00b
n00b


Joined: 03 Jan 2004
Posts: 20

PostPosted: Mon Jun 27, 2005 6:10 pm    Post subject: Email notification when user logs on via ssh Reply with quote

Ive searched and searched but cant find out how to get an email sent whenever some one logs on via ssh


so what i want is something that sends an email to say monitor@domain.com every time a user logs on.


can any one give me a few pointers or a solution ? im not a good programer so writing a script that goes threw the logs isnt an option.
Back to top
View user's profile Send private message
echto
Tux's lil' helper
Tux's lil' helper


Joined: 30 Jun 2002
Posts: 106

PostPosted: Mon Jun 27, 2005 10:13 pm    Post subject: Reply with quote

Quick and dirty.

tail -f /var/log/messages | grep sshd | grep opened | mail -s ssh your@emailaddr.com

8)
Back to top
View user's profile Send private message
Genone
Retired Dev
Retired Dev


Joined: 14 Mar 2003
Posts: 7742
Location: beyond the rim

PostPosted: Mon Jun 27, 2005 10:54 pm    Post subject: Reply with quote

I'd just hack something into /etc/profile.
Back to top
View user's profile Send private message
F4lC0N
n00b
n00b


Joined: 03 Jan 2004
Posts: 20

PostPosted: Mon Jun 27, 2005 11:40 pm    Post subject: Reply with quote

Genone wrote:
I'd just hack something into /etc/profile.



i was thinking something like that but didnt know how to do it

can you give me a pointer or two ?

i can email from the cli just getting it to send an email when some one logs in is the problem
Back to top
View user's profile Send private message
Genone
Retired Dev
Retired Dev


Joined: 14 Mar 2003
Posts: 7742
Location: beyond the rim

PostPosted: Tue Jun 28, 2005 12:31 am    Post subject: Reply with quote

When you're logged in with ssh it sets a few environment variables (SSH_CONNECTION, SSH_CLIENT and a few more), just test for those in a if statement in /etc/profile:
Code:
if [ -n "$SSH_CONNECTION" ]; then
    # send mail
fi

I've used something similar for example to restrict ssh shell access to root, but still allow cvs+ssh access to users (and no, using only keys wasn't an option).


Last edited by Genone on Tue Jun 28, 2005 3:20 am; edited 1 time in total
Back to top
View user's profile Send private message
F4lC0N
n00b
n00b


Joined: 03 Jan 2004
Posts: 20

PostPosted: Tue Jun 28, 2005 12:35 am    Post subject: Reply with quote

that sounds pretty good :)


I take it theres an enviroment variable that i can insert into the email to say what the user name is ?
Back to top
View user's profile Send private message
echto
Tux's lil' helper
Tux's lil' helper


Joined: 30 Jun 2002
Posts: 106

PostPosted: Tue Jun 28, 2005 12:36 am    Post subject: Reply with quote

Genone wrote:
Why you're logged in with ssh it sets a few environment variables (SSH_CONNECTION, SSH_CLIENT and a few more), just test for those in a if statement in /etc/profile:
Code:
if [ -n "$SSH_CONNECTION" ]; then
    # send mail
fi

I've used something similar for example to restrict ssh shell access to root, but still allow cvs+ssh access to users (and no, using only keys wasn't an option).


Good one! :D

I keep forgetting about /etc/profile.

I think I'll go walk into a fish market and yell "QUICK! Someone slap me with a wet trout!"

8)
Back to top
View user's profile Send private message
F4lC0N
n00b
n00b


Joined: 03 Jan 2004
Posts: 20

PostPosted: Tue Jun 28, 2005 3:16 am    Post subject: Reply with quote

Thanks heres what ive got

if [ -n "$SSH_CONNECTION" ]; then
echo "WARNING: user $USER has loged on " |mail -s"WARNING: user $USER has loged on " user@domain.com
fi


works like a charm
sends and email with the users name in when the ssh in and doesnt send one if the sftp in



Thanks for your help
Back to top
View user's profile Send private message
mach.82
Tux's lil' helper
Tux's lil' helper


Joined: 30 Oct 2003
Posts: 75
Location: 43°N/79°W

PostPosted: Mon Aug 08, 2005 10:51 pm    Post subject: Reply with quote

I don't have the mail command in my Gentoo system as I am using postfix!
Also, I don't think ssmtp and postfix can co-exist in the same system.
Q: What else can I use instead of the mail command with Postfix?
Thanks.
Back to top
View user's profile Send private message
zOOz
n00b
n00b


Joined: 24 Sep 2003
Posts: 27
Location: Lithuania

PostPosted: Mon Aug 08, 2005 11:48 pm    Post subject: Reply with quote

mach.82 wrote:
I don't have the mail command in my Gentoo system as I am using postfix!
Also, I don't think ssmtp and postfix can co-exist in the same system.
Q: What else can I use instead of the mail command with Postfix?
Thanks.


You can use mail-client/nail it is very nice and configurable.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Networking & Security All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum