12

I have a quesiton that puzzles me and I wonder if anyone has attempted to achieve the following:

Let's assume that this is the result of my 'last' command in a Linux environment:

root  pts/1        192.168.1.10      Wed Feb 10 07:04 - 07:57  (00:52)  
root  pts/2                          Tue Feb  9 22:00 - 00:13  (02:13)   

How can I setup a particular action (say for example a modified MOTD or sending an email) if the the 'root' user has logged in from 192.168.1.10. Is there a way of capturing this information?

The second part of this question is that how can I make the above check a bit more robust - i.e. if I have the following:

mary  pts/1        192.168.1.10      Wed Feb 10 07:04 - 07:57  (00:52)  
bob   pts/2                          Tue Feb  9 22:00 - 00:13  (02:13)      

Now I'd like to perform an action if the username is equal to 'mary' and the host is 192.168.1.10.

Any suggestions are welcomed.

Thank you in advance.

Tamas
  • 10,953
  • 13
  • 47
  • 77

6 Answers6

23

There's a special file /etc/ssh/sshrc where you can put some commands that will runs each time someone connect by ssh. I wrote that for you :

#!/bin/bash

mail=user@domain.tld
monitored_user=root
monitored_ip=x.x.x.x

hostname=$(hostname)

# add a welcome message:
printf >&2 "\nWelcome on $hostname $USER\n"

read -d " " ip <<< $SSH_CONNECTION

[[ $ip == $monitored_ip && $USER == $monitored_user ]] || exit 0

date=$(date "+%d.%m.%Y %Hh%M")
reverse=$(dig -x $ip +short)

mail -s "Connexion of $USER on $hostname" $mail <<EOF

IP: $ip
Reverse: $reverse
Date: $date
EOF

Put this script in a file, then put the full path of the script in /etc/ssh/sshrc

In man ssh :

/etc/ssh/sshrc : Commands in this file are executed by ssh when the user logs in, just before the user's shell (or command) is started. See the sshd(8) manual page for more information.

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • 1
    Put this script in a file, then put the full path of the script in `/etc/ssh/sshrc`, because sshrc file don't handle bash tests. POST edited accordingly. – Gilles Quénot Oct 08 '12 at 20:42
  • I get the same problem as @Sergey. In addition, it doesn't seem to like the `[[`. I tried `echo $SHELL` within the script to confirm it was bash. FWIW `[` works. Oh, and `dig` isn't installed. – Sparhawk Oct 02 '14 at 05:50
  • Because as I said before, you must put this script in a file ant not directly in `sshrc`. The `sshrc` must contains the full path of the executable script. – Gilles Quénot Oct 02 '14 at 11:34
  • This script is for all users, when they log in? – DarckBlezzer May 30 '17 at 23:26
  • Sure, test it and you will see – Gilles Quénot May 31 '17 at 08:42
  • i have tested this on raspberry pi os lite, and /etc/ssh/sshrc only seems to run upon login if there is no ~/.ssh/rc. Also, the existence of either of those files seems to stop rsync from other machines. – john-jones Dec 31 '20 at 17:53
1

Thanks for all your replies. Eventually I managed to find a solution which does work for the time being but it does have one flaw which I'll point out in a minute.

I have added the following to my /etc/bashrc file (or /etc/bash.bashrc whatever environment you're using):

HOST="192.168.0.1"
RHOST=`who am i | sed -n 's/.*(\([^) ]*\).*/\1/p; 1q'`
if [ "$RHOST" == "$HOST" ]; then
        echo "SAY WHAT!"
        #add further actions here if needed
fi

The flaw that I was talking about before may actually not be a flaw. If you're already SSH-ed into the system, and you want to SSH to a host which lives on the same IP, say ssh root@your-host who am i would then print 'your-host' but I think that's the way it should be.

Needless to say that the above sed statement can be modified so you can capture the username as well, and you can extend the if/else statement to suite your needs.

Thank you again for all your replies.

Tamas
  • 10,953
  • 13
  • 47
  • 77
  • Have you tested my `sshrc` file ? =) – Gilles Quénot Oct 08 '12 at 20:19
  • Hi sputnick. We are arriving to level 2 :) Do you know a way how to exclude logging the ssh commands that come from cronjobs/pieces of code. I'm only interested in user login from a console. Thanks. – Tamas Oct 10 '12 at 20:01
0

You can add something to /etc/profile or equivalent that does something depending on the value of $SSH_CLIENT.

Grisha Levit
  • 8,194
  • 2
  • 38
  • 53
0

It looks like you are using last because it reads /var/log/wtmp by default which is a record of logins. The who command also allows you to read the same file but with an interface more to your needs.

For example:

$ who --ips /var/log/wtmp | grep '^msw.*127.0.0.1'
msw      pts/2        2012-10-07 15:52 127.0.0.1
msw      pts/3        2012-10-07 15:55 127.0.0.1

where neither of those sessions were active, but rather historic and logged.

msw
  • 42,753
  • 9
  • 87
  • 112
0

In ubuntu i put a script in

/etc/profile.d

and when someone(user ssh) log in, it send an email to my mail

#/etc/profile.d/run_on_loggin.sh
echo $(who i am) | mail -s 'SSH Login Notification' mymail@hotmail.com

I want to create a php file with smtp, to send email with my mail to me... some times hotmail saved in spam...

if i have the php file i will run like this...

if i want to pass var to file php run like this...

excuse my english :3

note: i think this command run from user, be carefully if the user doen't has permission to use some command or send email.

DarckBlezzer
  • 4,578
  • 1
  • 41
  • 51
-1

One way would be to run a simple script periodically:

#!/bin/bash
users=$(last | sed -ne '/192\.168\.1\.10/ s/\([^ ]*\).*/\1/p')
for user in $users; do
    sendmail "$user" < email.txt
done

This would pipe the last command into sed to extract a user list and save it into the variable $users. The sed command uses the -n flag so it only prints what we tell it to. First, we select lines that contain the specified IP, with the /192\.168\.1\.10/ "address". On those lines, we attempt to extract the characters before a space, and if we succeed we print the result.

Then, we can loop through the $users variable and act accordingly.

One way to call this repeatedly would be through cron, and a simpler way would be to do while true; do ./my_script.bash; sleep 60; done.

  • Re cron vs while, instead use inotifywait to detect changes in /var/log/wtmp. – James Waldby - jwpat7 Oct 08 '12 at 19:46
  • Thanks Janito. I think there must be something more "interactive". My thinking is that if the lastlog gets updated with a value once someone ssh-s into the server, that very same information could be captured and then put into the /etc/bash.bashrc file so it'd be called at login. – Tamas Oct 08 '12 at 19:49