Skip to content

Instantly share code, notes, and snippets.

@kankadev
Created March 13, 2023 20:40
Show Gist options
  • Save kankadev/9477e22d6ef7826387d2cfab049f2705 to your computer and use it in GitHub Desktop.
Save kankadev/9477e22d6ef7826387d2cfab049f2705 to your computer and use it in GitHub Desktop.
[SSH Login Notification to Slack] This script notifies about SSH logins in a Slack channel #ssh #slack #linux
#!/bin/bash
# Add an incoming webhook in Slack first.
# Then save this script somewhere, e.g. /etc/ssh/scripts/notify_ssh_login_slack.sh and execute these commands once:
# chmod +x /etc/ssh/scripts/notify_ssh_login_slack.sh
# sudo echo "session optional pam_exec.so seteuid /etc/ssh/scripts/notify_ssh_login_slack.sh" >> /etc/pam.d/sshd
if [ "$PAM_TYPE" != "close_session" ]; then
URL="<slack-webhook-url>"
# Benutzername abrufen
USER=$(whoami)
# IP-Adresse abrufen
IP=$(hostname -I | awk '{print $1}')
HOST=$(hostname)
CONTENT="\"attachments\": [ { \"mrkdwn_in\": [\"text\", \"fallback\"], \"fallback\": \"SSH login: $PAM_USER connected to $HOST ($IP)\", \"text\": \"SSH login to $HOST ($IP)\", \"f>
curl -X POST --data-urlencode "payload={\"mrkdwn\": true, \"username\": \"SSH Notifications\", $CONTENT, \"icon_emoji\": \":inbox-tray:\"}" "$URL" &
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment