Web Hosting Talk







View Full Version : Email upon SSH login


MattF
05-03-2004, 10:19 AM
Despite dozens of searches, I can't for the live of me find the snippet of code used to email the admin whenever someone login via SSH, if I remember correctly it was appended to .profile.

Can someone point me in the right direction?

(wishes for natural language search)

choon
05-03-2004, 10:21 AM
Append this to the bottom of ~/.bash_profile:
# Send alert to server admin
echo 'ALERT - Root Shell Access on:' `date` `who` | mail -s "Alert: Root Access on Server `hostname` from `who | awk '{print $6}'`" YOUREMAIL
To test it, issue this command after adding the above to ~/.bash_profile:
source ~/.bash_profile
Hope this is what you are looking for ;)

Steven
05-03-2004, 10:22 AM
choon beat me to it

MattF
05-03-2004, 01:11 PM
Thanks guys

00000
03-30-2005, 05:35 PM
Originally posted by choon
Append this to the bottom of ~/.bash_profile:
# Send alert to server admin
echo 'ALERT - Root Shell Access on:' `date` `who` | mail -s "Alert: Root Access on Server `hostname` from `who | awk '{print $6}'`" YOUREMAIL
To test it, issue this command after adding the above to ~/.bash_profile:
source ~/.bash_profile
Hope this is what you are looking for ;)

I've done that before and it is working. Now I am trying to do the same on the new server but have a problem with saving file.

It doesn't allow me to save changes:
[ Could not open file for writing: Permission denied ]

Permissions on file are (it is the same on another server where I was able to save changes!):
-rw-r--r-- 1 root root 356 Aug 8 2004 .bash_profile


Please help.

Thanks!

sehe
03-30-2005, 05:55 PM
Originally posted by choon
Append this to the bottom of ~/.bash_profile:

whats the suggestion for ppl not using bash?
for csh it could be /etc/csh.login or ~/.login file
using /etc/profile would be nice, but not all shells do read it, beside it might be read on non-login invocation too
maybe abuse $HOME/.ssh/rc or /etc/ssh/sshrc?

eth00
03-30-2005, 06:13 PM
Originally posted by 00000
I've done that before and it is working. Now I am trying to do the same on the new server but have a problem with saving file.

It doesn't allow me to save changes:
[ Could not open file for writing: Permission denied ]

Permissions on file are (it is the same on another server where I was able to save changes!):
-rw-r--r-- 1 root root 356 Aug 8 2004 .bash_profile


Please help.

Thanks!

I had the same problem yesterday, you need to chattr -i .bash_profile.

HR Justin
03-30-2005, 07:28 PM
There is a better approach to this. One I can think of is using tenshi (http://www.gentoo.org/proj/en/infrastructure/tenshi/). The description for the utility is

Tenshi is a log monitoring program, designed to watch one or more log files for lines
matching user defined regular expressions and report on the matches........


You could write a small shell script to do the same if you don't need the more advanced features of this tool.

suhailc
03-31-2005, 08:40 AM
Don't forget to:

#chattr +i .bash_profile

corevps
03-31-2005, 08:54 AM
Might it not be better to put the code in /etc/bashrc ?

rfxn
03-31-2005, 01:13 PM
http://forums.ev1servers.net/showthread.php?p=279225#post279225

HR Justin
03-31-2005, 01:34 PM
Originally posted by rfxn
http://forums.ev1servers.net/showthread.php?p=279225#post279225

that isn't going to work when someone run commands without allocating a shell. To test this out, just do:

ssh servername "command here"

00000
03-31-2005, 07:45 PM
Originally posted by rfxn
http://forums.ev1servers.net/showthread.php?p=279225#post279225

Thanks, this works great!

linux-tech
03-31-2005, 08:18 PM
Jeeeeez, talk about dragging up a reaaaaaly old post ;)
adding this to ~/.bash_profile will work, but if you want to really be efficient, add it to something like /etc/profile, or create a script like /etc/profile.d/login.sh with the following

echo 'ALERT - Root Shell Access on:' `date` `who` | mail -s "Alert: Root Access on Server `hostname` from `who | awk '{print $6}'`" YOUREMAIL

The difference? This will let you know when ANY user logs in, not just when that specific user logs in. Of course, you'll need to make that an executable (chmod a+x /etc/profile.d/filename.sh), and make sure to get the .sh part in there as well :)

00000
04-01-2005, 06:24 AM
Originally posted by Microsoft Warrior
Jeeeeez, talk about dragging up a reaaaaaly old post ;)

Yes, that is happening when users are using search function :)