# /opt/notify-telegram.sh -rw-r--r-- 679 bytes View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/bash

# in /etc/pam.d/ssh:
# session [default=ignore] pam_exec.so /bin/bash /opt/notify-telegram.sh

# We want to trigger the script only when the SSH session starts.
# To be notified also when session closes, you can watch for 
# the "close_session" value.
if [[ "$PAM_TYPE" != "open_session" ]]; then
        exit 0
fi

TELEGRAM_API_KEY="YOUR_API_KEY"
CHAT_ID="YOUR_CHAT_ID"

read -r -d '' CONTENT <<-EOF || true
User logged in: ${PAM_USER} (${PAM_TYPE})
Now UTC: $(date --utc "+%F %T")
Env:
$(printenv)
EOF

curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_API_KEY}/sendMessage" \
        -F chat_id="${CHAT_ID}" \
        -F text="$CONTENT" > /dev/null || true