Track SSH Logins via Telegram. If you are a Dev-Ops Engineer, You and… | by Amith Chandrappa | Medium – Telegram 安装

Haley

Track SSH Logins via Telegram

If you are a Dev-Ops Engineer, You and Your team may want to keep a track of who is logging in to which Servers/Virtual Machines.

In this article, We will learn how to create a Telegram Bot and write a shell script to send messages to Telegram messenger using the Curl command.

Then you will also use this shell script to send a notification to your Telegram Channel when a SSH login is detected.

Building a Telegram Bot

Bots are third-party applications that run inside Telegram. Users can interact with bots by sending them messages, commands and inline requests. You control your bots using HTTPS requests to our Bot API.

To send a message to Telegram group or channel, you should first create your own bot. Just open Telegram, find @BotFather and type /start. Then follow instructions to create bot and get token to access the HTTP API.

  1. Search for @BotFather and click on “Start” or type “/start
  2. Type “/newbot” to start creating a new Bot
  3. Name the bot. Example, in our case we are naming the bot as “test-ssh-bot
  4. You will get a HTTP Access Token (marked in red). Make a note of it, You will have to use it in the shell script.
  5. Also, Tap on the link “ t.me/sample_test_ssh_bot” to message the bot. Send a test message to the bot.

Congratulations! Now at this point of time, You have a Bot ready.

Creating Telegram Chat Channel

Create a new Channel and name it as SSH Channel. Also add the bot we created in the last step to this channel as a subscriber.

After the Channel is created, Send a test message to the channel.

Get the required Details

Now that you have everything ready at Telegram’s side, Lets get the required details to write the Shell Script.

We need 2 things

  1. Bot Token — We have already got it
  2. Group Chat ID

Open the following link in any of the browser

https://api.telegram.org/bot<YourBOTToken>/getUpdates

You will see a json response, If you don’t see any data you should send a test chat message in the channel first. Highlighted in red is your Group Chat ID. Make a note of it, You will have to use it in the shell script.

Shell Script to send a Telegram message

To send a message we could use simple command like

curl https://api.telegram.org/bot<YourBOTToken>/sendMessage?chat_id=<group_chat_id>&text=<text_message>

Now, Lets build a Shell Script which can send a message on every SSH

Create file as /usr/local/bin/notify-on-ssh-login.shin the server which you want to monitor.

Give executable permission to this file

chmod 777 /usr/local/bin/notify-on-ssh-login.sh

Open the file sshd (vi /etc/pam.d/sshd) and add the following line

session optional pam_exec.so /usr/local/bin/notify-on-ssh-login.sh

Results

Congratulations!, You are all done. Now when anyone with the SSH Access logs into the VM, You will get a message as below.

Now you can add everyone in you Dev-Ops team to this Group.

Happy Coding!!

Ten articles before and after

977 Minutes to Days | Telegram

857 Months to Years | – Telegram 下载

20 Minutes to Days | Telegram

82380 Seconds to Hours | Telegram

89800 Seconds to Hours | Telegram

1373 Minutes to Days | Telegram

Telegram Bot integration with Jenkins | by Alok Srivastav | Medium – Telegram 安装

93 Minutes to Days | Telegram

28 Milliseconds to Minutes | – Telegram 下载

270 Minutes to Days | Telegram

About Me

Pretium lorem primis senectus habitasse lectus donec ultricies tortor adipiscing fusce morbi volutpat pellentesque consectetur risus curae malesuada dignissim lacus convallis massa mauris.