Skip to content

Instantly share code, notes, and snippets.

@rahmatov
Forked from matriphe/ssh-telegram.sh
Created January 1, 2018 11:18
Show Gist options
  • Save rahmatov/f682f3d4ea0ca511a0c87531a6022b8e to your computer and use it in GitHub Desktop.
Save rahmatov/f682f3d4ea0ca511a0c87531a6022b8e to your computer and use it in GitHub Desktop.
Bash Script to notify via Telegram Bot API when user log in SSH
# save it on /etc/profile/profile.d/
USERID="<user_or_group_id>"
KEY="<telegram_bot_api_key>"
URL="https://api.telegram.org/bot$KEY/sendMessage"
if [ -n "$SSH_CLIENT" ]; then
DATE_EXEC="$(date "+%d %b %Y %H:%M")"
IP=$(echo $SSH_CLIENT|awk '{print $1}')
PORT=$(echo $SSH_CLIENT|awk '{print $3}')
HOSTNAME=$(hostname -f)
TEXT="$DATE_EXEC: ${USER} logged in to $HOSTNAME from $IP on port $PORT"
curl -s -d "chat_id=$USERID&disable_web_page_preview=1&text=$TEXT" $URL > /dev/null
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment