Skip to content

Instantly share code, notes, and snippets.

@Alveel
Last active February 11, 2025 11:10
Show Gist options
  • Save Alveel/2dc37ae8155b7bf4edd5fe964dfa0498 to your computer and use it in GitHub Desktop.
Save Alveel/2dc37ae8155b7bf4edd5fe964dfa0498 to your computer and use it in GitHub Desktop.
PAM SSH login Discord & Telegram notification script
# ...
# At/near the bottom, add:
# Login notifications
session required pam_exec.so /usr/local/sbin/sshlogin-alert.sh
# Standard Un*x password updating.
@include common-password
#!/bin/bash
#
# Requires curl and jq
pushover_app_token="mytoken"
pushover_user_key="userkey"
pushover_priority="0"
pushover_api_url="https://api.pushover.net/1/messages.json"
discord_webhook_url="https://discord.com/api/webhooks/1234/abcd"
telegram_webhook_url="https://api.telegram.org/bot1234:abcd"
telegram_chat_id="5678"
if ! [ "$PAM_TYPE" = "open_session" ]; then
exit 0
fi
# User has been identified and login may proceed
message="SSH login: $PAM_USER@$(uname --nodename) | Service: $PAM_SERVICE | TTY: $PAM_TTY | $(date) "
send_pushover() {
curl --silent \
--form-string "token=$pushover_app_token" \
--form-string "user=$pushover_user_key" \
--form-string "message=$message" \
--form-string "priority=$pushover_priority" \
"$pushover_api_url"
}
send_discord() {
json_body=$(jq -n --arg msg "$message" '{content: $msg}')
curl --silent --request POST --header "Content-Type: application/json" --data "$json_body" "$discord_webhook_url"
}
send_telegram() {
json_body=$(jq -n --arg chat_id "$telegram_chat_id" --arg msg "$message" '{chat_id: $chat_id, text: $msg}')
curl --silent --request POST --header "Content-Type: application/json" --data "$json_body" "$telegram_webhook_url/sendMessage"
}
send_pushover
send_discord
send_telegram
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment