Writing a small bash script to send a notification in my telegram monitoring channel each time a user logs in to one of my server
I thought it was a nice little idea to set up a script that notifies my when a user logins into one of my most important VMs/CTs/Hosts. Of course there are probably ways around it, but it was somewhat fun to write. So let's dive in!
For the notifications, I choose Telegram, mostly because I already have most of my notifications in there already and because the API is just so simple 😲
Telegram makes it ridiculously easy to create a new bot, so I just dm-ed @BotFather and got a new token.
Subsequently, all, I had to do is add the bot to the group I use for monitoring. Then I could use the API myself to determine the chat ID of my group:
I simply went to
https://api.telegram.org/botTELEGRAMBOTTOKEN/getUpdates in my browser (with
TELEGRAMBOTTOKEN being the real token) and got the chat id:
On Telegram, chat ID are negative, and user IDs are positive, so it was pretty easy to find.
I then ran:
With the correct variables, and it worked, nice 😀
After having a way to send messages, I need to generate what I need to send.
So, I wrote a little script to combine everything and send the message:
Did a quick test, it worked, nice
I'm starting to use Ansible to deploy configs to my servers, so I just created a new role with this task:
And added said role the playbook.yaml (parts are redacted, hence why it's in different blocks)
This is a very little script that, honestly, is just a gimmick, there definitively are ways to bypass it (for example Ansible doesn't trigger it because it doesn't load the profile).
I wrote it mainly to track logins of one machine which is exposed to the outside for me to use as a bastion.
Want to chat about this article? Just post a message down here. Chat is powered by giscus and all discussions can be found here: TheStaticTurtle/blog-comments