Skip to content

Instantly share code, notes, and snippets.

@alessandrocuda
Last active January 23, 2022 02:34
Show Gist options
  • Save alessandrocuda/88b83a83000a04e1e01df68731d33a76 to your computer and use it in GitHub Desktop.
Save alessandrocuda/88b83a83000a04e1e01df68731d33a76 to your computer and use it in GitHub Desktop.
Shell Script that sends alerts at login with sendmail and PAM https://medium.com/@alessandrocuda/ssh-login-alerts-with-sendmail-and-pam-3ef53aca1381
#!/bin/sh
# Your Email Information: Recipient (To:), Subject and Body
RECIPIENT="yourAddr@email.com"
SUBJECT="Email from your Server: SSH Alert"
BODY="
A SSH login was successful, so here are some information for security:
User: $PAM_USER
User IP Host: $PAM_RHOST
Service: $PAM_SERVICE
TTY: $PAM_TTY
Date: `date`
Server: `uname -a`
"
if [ ${PAM_TYPE} = "open_session" ]; then
echo "Subject:${SUBJECT} ${BODY}" | /usr/sbin/sendmail ${RECIPIENT}
fi
exit 0
@m0n4
Copy link

m0n4 commented Aug 8, 2020

Thanks
In order to make it work on my debian 9 i just edited few things:

  • Change the first line of the BODY so that it does not contain a colon (otherwise Sendmail generates an email with a blank body)
  • Add quotes to PAM_TYPE: "${PAM_TYPE}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment