Access to linux server using Telegram bot in Python

Quite often there are situations when access to the server is needed here and now. However, connecting via SSH is not always the most convenient way, because you may not have an SSH client, server address, or user/password combination at hand. Of course have Webmin, which simplifies administration, but it also does not provide instant access.

So I decided to implement a simple but interesting solution. Namely, to write a Telegram bot that, when launched on the server itself, will execute the commands sent to it and return the result. Having studied some articles on this topic, I realized that no one has yet described such implementations.

I implemented this project on Ubuntu 16.04, but for a trouble-free run on other distributions, I tried to make everything in a general way.

Bot registration

Register a new bot with @BotFather. We send him /newbot and further in the text. We will need a new bot token and your id (you can get it, for example, from @userinfobot).

Python preparation

To run the bot, we will use the library telebot (pip install pytelegrambotapi). Using the library subprocess we will execute commands on the server.

Bot launch

Create a bot.py file on the server:
nano bot.py

And paste the code into it:

from subprocess import check_output
import telebot
import time

bot = telebot.TeleBot("XXXXXXXXX:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")#Ρ‚ΠΎΠΊΠ΅Π½ Π±ΠΎΡ‚Π°
user_id = 0 #id вашСго Π°ΠΊΠΊΠ°ΡƒΠ½Ρ‚Π°
@bot.message_handler(content_types=["text"])
def main(message):
   if (user_id == message.chat.id): #провСряСм, Ρ‡Ρ‚ΠΎ ΠΏΠΈΡˆΠ΅Ρ‚ ΠΈΠΌΠ΅Π½Π½ΠΎ Π²Π»Π°Π΄Π΅Π»Π΅Ρ†
      comand = message.text  #тСкст сообщСния
      try: #Ссли ΠΊΠΎΠΌΠ°Π½Π΄Π° нСвыполняСмая - check_output выдаст exception
         bot.send_message(message.chat.id, check_output(comand, shell = True))
      except:
         bot.send_message(message.chat.id, "Invalid input") #Ссли ΠΊΠΎΠΌΠ°Π½Π΄Π° Π½Π΅ΠΊΠΎΡ€Ρ€Π΅ΠΊΡ‚Π½Π°
if __name__ == '__main__':
    while True:
        try:#добавляСм try для бСспСрСбойной Ρ€Π°Π±ΠΎΡ‚Ρ‹
            bot.polling(none_stop=True)#запуск Π±ΠΎΡ‚Π°
        except:
            time.sleep(10)#Π² случаС падСния

We replace the bot token in it with the one issued by @BotFather, and user_id with the id value of your account. Checking the user id is needed so that the bot provides access to your server only to you. Function check_output() executes the given command and returns the result.

It remains only to start the bot. For running processes on the server, I prefer to use screen (sudo apt-get install screen):

screen -dmS ServerBot python3 bot.py

(where "ServerBot" is the process ID)

The process will automatically run in the background. Let's go to the dialogue with the bot and check that everything works as it should:

Access to linux server using Telegram bot in Python

Congratulations! The bot executes the commands sent to it. Now, in order to access the server, you just need to open a dialogue with the bot.

Command repetition

Often, to monitor the state of the server, you have to execute the same commands. Therefore, the implementation of repeating commands without resending them will be very out of place.

We will implement it using inline buttons under the messages:

from subprocess import check_output
import telebot
from telebot import types #ДобавляСм ΠΈΠΌΠΏΠΎΡ€Ρ‚ ΠΊΠ½ΠΎΠΏΠΎΠΊ
import time

bot = telebot.TeleBot("XXXXXXXXX:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")#Π’ΠΎΠΊΠ΅Π½ Π±ΠΎΡ‚Π°
user_id = 0 #id вашСго Π°ΠΊΠΊΠ°ΡƒΠ½Ρ‚Π°
@bot.message_handler(content_types=["text"])
def main(message):
   if (user_id == message.chat.id): #провСряСм, Ρ‡Ρ‚ΠΎ ΠΏΠΈΡˆΠ΅Ρ‚ ΠΈΠΌΠ΅Π½Π½ΠΎ Π²Π»Π°Π΄Π΅Π»Π΅Ρ†
      comand = message.text  #тСкст сообщСния
      markup = types.InlineKeyboardMarkup() #создаСм ΠΊΠ»Π°Π²ΠΈΠ°Ρ‚ΡƒΡ€Ρƒ
      button = types.InlineKeyboardButton(text="ΠŸΠΎΠ²Ρ‚ΠΎΡ€ΠΈΡ‚ΡŒ", callback_data=comand) #создаСм ΠΊΠ½ΠΎΠΏΠΊΡƒ
      markup.add(button) #добавляСм ΠΊΠ½ΠΎΠΏΠΊΡƒ Π² ΠΊΠ»Π°Π²ΠΈΠ°Ρ‚ΡƒΡ€Ρƒ
      try: #Ссли ΠΊΠΎΠΌΠ°Π½Π΄Π° нСвыполняСмая - check_output выдаст exception
         bot.send_message(user_id, check_output(comand, shell = True,  reply_markup = markup)) #Π²Ρ‹Π·Ρ‹Π²Π°Π΅ΠΌ ΠΊΠΎΠΌΠ°Π½Π΄Ρƒ ΠΈ отправляСм сообщСниС с Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ΠΎΠΌ
      except:
         bot.send_message(user_id, "Invalid input") #Ссли ΠΊΠΎΠΌΠ°Π½Π΄Π° Π½Π΅ΠΊΠΎΡ€Ρ€Π΅ΠΊΡ‚Π½Π°

@bot.callback_query_handler(func=lambda call: True)
def callback(call):
  comand = call.data #считываСм ΠΊΠΎΠΌΠ°Π½Π΄Ρƒ ΠΈΠ· поля ΠΊΠ½ΠΎΠΏΠΊΠΈ data
  try:#Ссли ΠΊΠΎΠΌΠ°Π½Π΄Π° Π½Π΅ выполняСмая - check_output выдаст exception
     markup = types.InlineKeyboardMarkup() #создаСм ΠΊΠ»Π°Π²ΠΈΠ°Ρ‚ΡƒΡ€Ρƒ
     button = types.InlineKeyboardButton(text="ΠŸΠΎΠ²Ρ‚ΠΎΡ€ΠΈΡ‚ΡŒ", callback_data=comand) #создаСм ΠΊΠ½ΠΎΠΏΠΊΡƒ ΠΈ Π² data ΠΏΠ΅Ρ€Π΅Π΄Π°Ρ‘ΠΌ ΠΊΠΎΠΌΠ°Π½Π΄Ρƒ
     markup.add(button) #добавляСм ΠΊΠ½ΠΎΠΏΠΊΡƒ Π² ΠΊΠ»Π°Π²ΠΈΠ°Ρ‚ΡƒΡ€Ρƒ
     bot.send_message(user_id, check_output(comand, shell = True), reply_markup = markup) #Π²Ρ‹Π·Ρ‹Π²Π°Π΅ΠΌ ΠΊΠΎΠΌΠ°Π½Π΄Ρƒ ΠΈ отправляСм сообщСниС с Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ΠΎΠΌ
  except:
     bot.send_message(user_id, "Invalid input") #Ссли ΠΊΠΎΠΌΠ°Π½Π΄Π° Π½Π΅ΠΊΠΎΡ€Ρ€Π΅ΠΊΡ‚Π½Π°

if __name__ == '__main__':
    while True:
        try:#добавляСм try для бСспСрСбойной Ρ€Π°Π±ΠΎΡ‚Ρ‹
            bot.polling(none_stop=True)#запуск Π±ΠΎΡ‚Π°
        except:
            time.sleep(10)#Π² случаС падСния

Restarting the bot:

killall python3
screen -dmS ServerBot python3 bot.py

Let's check again that everything works correctly:

Access to linux server using Telegram bot in Python

By pressing the button under the message, the bot must repeat the command from which this message was sent.

Instead of a conclusion

Of course, this method does not pretend to be a replacement for the classic connection methods, however, it allows you to quickly find out about the state of the server and send commands to it that do not require complex output.

Source: habr.com

Add a comment