Skip to content

Instantly share code, notes, and snippets.

@kabachook
Created April 13, 2018 17:07
Show Gist options
  • Save kabachook/84ae19f6647c5ac5fcf206e54969fb1d to your computer and use it in GitHub Desktop.
Save kabachook/84ae19f6647c5ac5fcf206e54969fb1d to your computer and use it in GitHub Desktop.
Telegram SSH login notifier

Installation

  • Add notify.sh to some place
  • Change token and chat_id values (you can get chat_id, e.g. by writing to bot and requesting getUpdates)
  • chmod +x /path/to/notify.sh
  • echo "session optional pam_exec.so seteuid /path/to/notify.sh" >> /etc/pam.d/sshd

You can change optional to required if you sure that everything works

#!/bin/sh
# Change these two lines:
token=""
chat_id="11111"
if [ "$PAM_TYPE" != "close_session" ]; then
host="`hostname`"
message="SSH login $PAM_USER from $PAM_RHOST on $host"
curl "https://api.telegram.org/bot$token/sendMessage?chat_id=$chat_id&text=$message"
message="`env`"
curl --data-urlencode "text=$message" "https://api.telegram.org/bot$token/sendMessage?chat_id=$chat_id"
fi
# Append this to /etc/pam.d/sshd
session optional pam_exec.so seteuid /etc/ssh/notify.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment