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
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
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
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:
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:
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