Skip to content

Instantly share code, notes, and snippets.

@Mehran
Last active May 31, 2019 17:27
Show Gist options
  • Save Mehran/f1df57b342c54a0ac019deaaf4a809b8 to your computer and use it in GitHub Desktop.
Save Mehran/f1df57b342c54a0ac019deaaf4a809b8 to your computer and use it in GitHub Desktop.
Bot that posts SSH logins to Telegram
#!/bin/bash
KEY="<your bot token>"
URL="https://api.telegram.org/bot$KEY/sendMessage"
TARGET="<your chat ID>" # Telegram ID of the conversation with the bot, get it from /getUpdates API
TEXT="User *$PAM_USER* logged in on *$HOSTNAME* at $(date '+%Y-%m-%d %H:%M:%S %Z')
Remote host: $PAM_RHOST
Remote user: $PAM_USER
Service: $PAM_SERVICE
TTY: $PAM_TTY"
PAYLOAD="chat_id=$TARGET&text=$TEXT&parse_mode=Markdown&disable_web_page_preview=true"
# Run in background so the script could return immediately without blocking PAM
curl -s --max-time 10 --retry 5 --retry-delay 2 --retry-max-time 10 -d "$PAYLOAD" $URL > /dev/null 2>&1 &
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment