Be sure to connect via SSH to a second session in case there's a problem
Create the script file.
sudo nano /etc/ssh/login-notify.sh
Paste in contents of login-notify.sh file below.
Allow script to be executed.
sudo chmod +x /etc/ssh/login-notify.sh
Now modify /etc/pam.d/sshd to add script call
sudo nano /etc/pam.d/sshd
Paste in this line below existing session optional commands:
# Send notification on user connect
session optional pam_exec.so seteuid /etc/ssh/login-notify.sh
For testing purposes, the module is included as optional, so that you can still log in if the execution fails. After you made sure that it works, you can change optional to required. Then login won't be possible unless the execution of your hook script is successful (if that is what you want).
What do I need to change in the script to remove all the env stuff? I tried to comment it out but then the script did not send any notifications. I really only need the user and ip part.
Edit: And I found out that I have sync script that syncs a files between two host every 5 min - this creates a lot of notifications! Can I excempt users?? (please :D )