Skip to content

Instantly share code, notes, and snippets.

@a-maumau
Last active July 18, 2021 18:02
Show Gist options
  • Save a-maumau/1d85362449dd5771ddfbe9106b201d3f to your computer and use it in GitHub Desktop.
Save a-maumau/1d85362449dd5771ddfbe9106b201d3f to your computer and use it in GitHub Desktop.
notify login via slack on linux
#!/bin/bash
# you also need to set this script in `pam.d/<what you want>`.
# for example,
# in file `/etc/pam.d/common-password`
# add following:
#
# session optional pam_exec.so /usr/local/bin/notify_login.sh
#
# the file names under the `pam.d` may differ in OS and version,
# please suit your enviroment.
HOST_NAME="host_name"
SLACK_WEBHOOK_API_URL='your_slack_webhook_url'
USER_NAME="user: $PAM_USER"
#RUSER="Ruser: $PAM_RUSER"
REMOTE_HOST="from: $PAM_RHOST"
#SERVER_NAME="server: `uname -a`"
SERVICE="Service: $PAM_SERVICE"
TTY="TTY: $PAM_TTY"
DATE="Date: `date`"
if [ "$PAM_TYPE" = "open_session" ]; then
JSON="{ \"text\": \"\`[LOGIN][$HOST_NAME]\`\n\`\`\`$DATE\n$USER_NAME\n$REMOTE_HOST\n$SERVICE\n$TTY\`\`\`\" }"
RET=`curl -s -X POST -H 'Content-Type: application/json' -d "$JSON" $SLACK_WEBHOOK_API_URL`
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment