Last active
March 4, 2022 05:21
-
-
Save xdung24/efe16c77f167558ff595f467d6f2a056 to your computer and use it in GitHub Desktop.
Notify ssh connection via telegram bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### 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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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