You are not logged in.

#1 2008-09-24 03:39:51

windowbreaker
Member
Registered: 2008-06-18
Posts: 46

how to trigger a text msg when ssh login occurs

How can I setup my box to text me whenever anyone logs in via ssh?  I have an SMTP server and know how to shoot an email, just not sure how to get sshd to trigger that event.

Offline

#2 2008-09-24 04:06:55

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,223
Website

Re: how to trigger a text msg when ssh login occurs

Create a global bashrc file and make sure all users .bashrc sources that file. For example, I have /etc/bashrc which does EVERYTHING I want, and I just make every users .bashrc source it:   . /etc/bashrc

Obviously will only work if users use bash as their shell, and you trust them not to edit their .bashrc to remove the sourcing of the global file.

Offline

#3 2008-09-24 05:21:32

dav7
Member
From: Australia
Registered: 2008-02-08
Posts: 674

Re: how to trigger a text msg when ssh login occurs

For added security, change all users' shells from bash or whatever they use to a script. Whatever component handles logins will see the script and execute it with (I think) 'sh', so it assumedly needs to be written for sh.

Whatever's in the script will be run regardless of user preference as the script is registered as their shell, sooo... big_smile

Be sure to end the script with 'bash' so the user actually gets a shell!

Notes:
- The 'chsh' utility can be used to change current accounts' shells.
- Unless the location and name of your script is specified in /etc/shells, chsh will not be able to set it as a shell unless you run chsh under the root account.
- The 'adduser' utility, if you use it, is a Perl script located in /usr/sbin, and the default shell can easily be set to your script by changing the 'defshell' variable near the top.

-dav7

Last edited by dav7 (2008-09-24 05:22:30)


Windows was made for looking at success from a distance through a wall of oversimplicity. Linux removes the wall, so you can just walk up to success and make it your own.
--
Reinventing the wheel is fun. You get to redefine pi.

Offline

#4 2008-09-24 06:42:44

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,223
Website

Re: how to trigger a text msg when ssh login occurs

dav7 wrote:

change all users' shells from bash or whatever they use to a script.

What an excellent solution smile

dav7 wrote:

Whatever component handles logins will see the script and execute it with (I think) 'sh', so it assumedly needs to be written for sh.

Presumably it would run at whatever is at the start of the script. Eg:
#!/bin/bash
or
#!/bin/sh
or
#!/bin/dash
etc etc

Offline

#5 2008-09-24 12:18:58

bender02
Member
From: UK
Registered: 2007-02-04
Posts: 1,328

Re: how to trigger a text msg when ssh login occurs

Or run a daemon that monitors /var/log/auth.log.

Offline

#6 2008-09-24 16:18:13

windowbreaker
Member
Registered: 2008-06-18
Posts: 46

Re: how to trigger a text msg when ssh login occurs

All good ideas.  I will attempt to write a logfile monitoring script in perl.  I don't like the idea of getting a text message whenever someone logs in, as there are more local logins than remote SSH logins.  For some reason I thought either sshd or tcpd could trigger a script to notify me.  In fact, is there an easy way to setup a file in /etc/xinetd.d for sshd and have it call a wrapper script that pages me with the user/ip?

Offline

#7 2008-09-26 07:06:33

xvalentinex
Member
Registered: 2008-05-31
Posts: 22

Re: how to trigger a text msg when ssh login occurs

This is an interesting idea, so I did some research.

My first thought was to have a udev rule RUN a program on the creation of /dev/pts/X, but I couldn't get that to work.  I'm not very good with udev though.

Another option is to stick with the .bashrc idea.  If you type 'env' in your shell while SSH'ed in, you'll see a few environment variables dealing with SSH that you could right a script around.  Even include the connecting IP of the SSH session in the text.  This one works and is fairly simple.

#if SSH_CLIENT defined run text.sh with $SSH_CLIENT as an argument
if [[ -n ${SSH_CLIENT} ]]; then
        /usr/local/bin/text.sh $SSH_CLIENT
fi

If you use keys to authenticate there is a way to have ssh run a forced command upon login.  Details are here:
http://oreilly.com/catalog/sshtdg/chapter/ch08.html

Also you could use the ForceCommand option in sshd_config

Just remember that with forced commands you want to push them to a script which then ends by executing a shell.
#!/bin/bash
command to text
/bin/bash

Last edited by xvalentinex (2008-09-26 07:15:27)

Offline

#8 2008-09-28 03:55:15

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: how to trigger a text msg when ssh login occurs

Should probably change that to "exec /bin/bash"

Offline

Board footer

Powered by FluxBB