- you need to create app https://api.slack.com/apps/new
- enable webhook on your app https://api.slack.com/apps/xxxxxxx/incoming-webhooks?
- checkout your webhook url and copy https://hooks.slack.com/services/xxxxxx/xxxxx/xxxxx
- your SLACK_WEBHOOK_URL is https://hooks.slack.com/services/xxxxxx/xxxxx/xxxxx
- create
ssh-bot
channel
- Add ssh script file
sudo vim /etc/ssh/scripts/notify.sh
#!/bin/sh
url="SLACK_WEBHOOK_URL"
if [ "$PAM_TYPE" != "close_session" ]; then
channel="#ssh-bot"
host="`hostname`"
content="\"attachments\": [ { \"mrkdwn_in\": [\"text\", \"fallback\"], \"fallback\": \"SSH login: $PAM_USER connected to \`$host\`\", \"text\": \"SSH login to \`$host\`\", \"fields\": [ { \"title\": \"User\", \"value\": \"$PAM_USER\", \"short\": true }, { \"title\": \"IP Address\", \"value\": \"$PAM_RHOST\", \"short\": true } ], \"color\": \"#F35A00\" } ]"
curl -X POST --data-urlencode "payload={\"channel\": \"$channel\", \"mrkdwn\": true, \"username\": \"ssh-bot\", $content, \"icon_emoji\": \":ghost:\"}" $url
fi
- Give script permission
sudo chmod +x /etc/ssh/scripts/notify.sh
- Add script to pam.d
sudo echo "session optional pam_exec.so seteuid /etc/ssh/scripts/notify.sh" >> /etc/pam.d/sshd