Skip to content

Instantly share code, notes, and snippets.

@Kreumadragon
Last active January 28, 2017 20:15
Show Gist options
  • Select an option

  • Save Kreumadragon/c2ea84da0c85461ba73194f77d85b0e3 to your computer and use it in GitHub Desktop.

Select an option

Save Kreumadragon/c2ea84da0c85461ba73194f77d85b0e3 to your computer and use it in GitHub Desktop.
Own login notification via telegram bot api. (needs jq, apt install jq)
#Place this file under /etc/profile.d/ to get notifyed whenever someone goes through a login procedure in ssh
USERID="<the chatID where you want to get notificated>"#use api.telegram.org/bot$KEY/getUpdate to find out the ID
KEY="<your bot api-key here"
TIMEOUT="10"
URL="https://api.telegram.org/bot$KEY/sendMessage"
DATE_EXEC="$(date "+%d %b %Y %H:%M")"
#TMPFILE='/tmp/ipinfo-$DATE_EXEC.txt' Only for additional IPLookup
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}')
#Need JQ to work (apt install jq) Only for additional IPLookup
# curl http://ipinfo.io/$IP -s -o $TMPFILE
# CITY=$(cat $TMPFILE | jq '.city' < $TMPFILE -r)
# REGION=$(cat $TMPFILE | jq '.region' < $TMPFILE -r)
# COUNTRY=$(cat $TMPFILE | jq '.country' < $TMPFILE -r)
# ORG=$(cat $TMPFILE | jq '.org' < $TMPFILE -r)
TEXT="$DATE_EXEC: ${USER} hat sich auf $HOSTNAME ($IPADDR) von der IP $IP auf den Port $PORT angemeldet.%0ahttps://db-ip.com/$IP"
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