Skip to content

Instantly share code, notes, and snippets.

@ix4
Created July 5, 2021 18:27
Show Gist options
  • Save ix4/d3799f217ed10569764f61b847eae046 to your computer and use it in GitHub Desktop.
Save ix4/d3799f217ed10569764f61b847eae046 to your computer and use it in GitHub Desktop.

Send message to Telegram on any SSH login

#!/bin/bash
# prepare any message you want
login_ip="$(echo $SSH_CONNECTION | cut -d " " -f 1)"
login_date="$(date +"%e %b %Y, %a %r")"
login_name="$(whoami)"
# For new line I use $'\n' here
message="New login to server"$'\n'"$login_name"$'\n'"$login_ip"$'\n'"$login_date"
#send it to telegram
telegram-send "$message"
#!/bin/bash
GROUP_ID=<group_id>
BOT_TOKEN=<bot_token>
# this 3 checks (if) are not necessary but should be convenient
if [ "$1" == "-h" ]; then
echo "Usage: `basename $0` \"text message\""
exit 0
fi
if [ -z "$1" ]
then
echo "Add message text as second arguments"
exit 0
fi
if [ "$#" -ne 1 ]; then
echo "You can pass only one argument. For string with spaces put it on quotes"
exit 0
fi
curl -s --data "text=$1" --data "chat_id=$GROUP_ID" 'https://api.telegram.org/bot'$BOT_TOKEN'/sendMessage' > /dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment