Successful login
Try this bash script to get user name and user IP address. It is example only, you can modify it as needed. Works only for logged-in users.
#!/bin/bash
if [ -n "$SSH_CLIENT" ]; then
SSHIP=$(echo $SSH_CLIENT | cut -f 1 -d ' ')
SSHUSER=$(w | grep "$SSHIP" | cut -f 1 -d ' ')
echo -e " USER=$SSHUSER \n IP=$SSHIP"
# curl and next commands may be placed here ...
fi
!!! Note that the script only works well with at most one ssh connection !!!
For multiple concurrent connections, it would need to be modified, it would become more complex.
Unsuccessful login
Failed SSH log-in attempts you can find in the '/var/log/auth.log' file.
Here is example how to list unsuccessful SSH attempts:
grep -i "ssh" /var/log/auth.log | grep "fail"
The output lines contain user=
and rhost=
values which identify user and his IP address.
Here is an example of filter to simplify log output:
grep -i "ssh" /var/log/auth.log | awk 'BEGIN { FS="rhost="; } /failure;/{ print $2; }'
Important note
I think it's not a good idea to send an email after every unsuccessful login. When your computer interface is exposed to the Internet, it will log hundreds to thousands of failed logon attempts every day.