Skip to content

Instantly share code, notes, and snippets.

@xdung24
Last active March 4, 2022 05:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xdung24/efe16c77f167558ff595f467d6f2a056 to your computer and use it in GitHub Desktop.
Save xdung24/efe16c77f167558ff595f467d6f2a056 to your computer and use it in GitHub Desktop.
Notify ssh connection via telegram bot
### Telegram notification when user login over ssh
- create telegram bot (copy bot token) add to line 3
- get chat id (user, group, channel) add to line 2
- "sudo apt install jq"
- copy to /etc/profile.d
- "sudo chmod +x ssh-notification.sh"
# Edit this
USERID="TELEGRAM_CHAT_ID"
KEY="TELEGRAM_BOT_AUTH_KEY"
TIMEOUT="10"
URL="https://api.telegram.org/bot$KEY/sendMessage"
DATE_EXEC="$(date "+%d %b %Y %H:%M")"
if [ -n "$SSH_CLIENT" ]; then
IP=$(echo $SSH_CLIENT | awk '{print $1}')
PORT=$(echo $SSH_CLIENT | awk '{print $3}')
HOSTNAME=$(hostname -f)
IPADDR=$(hostname -I | awk '{print $1}')
JSON=$(curl -s http://ipinfo.io/$IP)
CITY=$(echo $JSON | jq '.city')
REGION=$(echo $JSON | jq '.region')
COUNTRY=$(echo $JSON | jq '.country')
ORG=$(echo $JSON | jq '.org')
TEXT="$DATE_EXEC: ${USER} logged in to $HOSTNAME ($IPADDR) from $IP - $ORG - $CITY, $REGION, $COUNTRY on port $PORT"
curl -s --max-time $TIMEOUT -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