Created
July 8, 2019 14:00
-
-
Save anir0y/fb81ba1a46b37b10b504d08e6ea6c38d to your computer and use it in GitHub Desktop.
read about it here: http://anir0y.github.io/class/blog/bash-get-ssh-login-notification-on-telegram/
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
USERID="<Enter_the_user_id_of_the_recipient>" | |
KEY="<The_key_of_your_telegram_bot>" | |
TIMEOUT="10" | |
URL="https://api.telegram.org/bot$KEY/sendMessage" | |
DATE_EXEC="$(date "+%d %b %Y %H:%M")" #Collect date & time. | |
TMPFILE='/tmp/ipinfo-$DATE_EXEC.txt' #Create a temporary file to keep data in. | |
if [ -n "$SSH_CLIENT" ] && [ -z "$TMUX" ]; then #Trigger | |
IP=$(echo $SSH_CLIENT | awk '{print $1}') #Get Client IP address. | |
PORT=$(echo $SSH_CLIENT | awk '{print $3}') #Get SSH port | |
HOSTNAME=$(hostname -f) #Get hostname | |
IPADDR=$(hostname -I | awk '{print $1}') | |
curl https://ipinfo.io/$IP -s -o $TMPFILE #Get info on client IP. | |
CITY=$(cat $TMPFILE | sed -n 's/^ "city":[[:space:]]*//p' | sed 's/"//g') #Client IP info parsing | |
REGION=$(cat $TMPFILE | sed -n 's/^ "region":[[:space:]]*//p' | sed 's/"//g') | |
COUNTRY=$(cat $TMPFILE | sed -n 's/^ "country":[[:space:]]*//p' | sed 's/"//g') | |
ORG=$(cat $TMPFILE | sed -n 's/^ "org":[[:space:]]*//p' | sed 's/"//g') | |
TEXT="$DATE_EXEC: ${USER} logged in to $HOSTNAME ($IPADDR) from $IP - $ORG - $CITY, $REGION, $COUNTRY port $PORT" | |
curl -s --max-time $TIMEOUT -d "chat_id=$USERID&disable_web_page_preview=1&text=$TEXT" $URL > /dev/null | |
rm $TMPFILE #clean up after | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment