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).