load-net

linux_server_opros

May 26th, 2021 (edited)
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.71 KB | None | 0 0
  1. ECHO bot centos 8
  2. dnf install gcc openssl-devel bzip2-devel libffi-devel zlib-devel wget
  3. cd /usr/src
  4. wget https://www.python.org/ftp/python/3.9.5/Python-3.9.5.tgz
  5. tar xzf Python-3.9.5.tgz
  6. cd Python-3.9.5
  7. ./configure --enable-optimizations
  8. make altinstall
  9. python3.9 -m pip install --upgrade pip
  10. /////////////////////////////////////////////////////////////////////
  11. Виртуальное окружение python
  12. встать над каталогом
  13. python3.9 -m venv python3.9-venv
  14. source python3.9-venv/bin/activate
  15. pip3 install pyTelegramBotAPI
  16. deactivate
  17. /////////////////////////////////////////////////////////////////////
  18.  
  19. python3.9 -m pip install aiogram
  20. python3.9 -V
  21.  
  22. mkdir /home/bot
  23. mcedit /home/bot/config.py
  24. TOKEN = '123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11'
  25.  
  26. mcedit /home/bot/bot.py
  27.  
  28. from aiogram import Bot, types
  29. from aiogram.dispatcher import Dispatcher
  30. from aiogram.utils import executor
  31.  
  32. from config import TOKEN
  33.  
  34. bot = Bot(token=TOKEN)
  35. dp = Dispatcher(bot)
  36.  
  37. @dp.message_handler(commands=['start'])
  38. async def process_start_command(message: types.Message):
  39.     await message.reply("Привет!\nНапиши мне что-нибудь!")
  40.  
  41. @dp.message_handler(commands=['help'])
  42. async def process_help_command(message: types.Message):
  43.     await message.reply("Напиши мне что-нибудь, и я отпрпавлю этот текст тебе в ответ!")
  44.  
  45. @dp.message_handler()
  46. async def echo_message(msg: types.Message):
  47.     await bot.send_message(msg.from_user.id, msg.text)
  48.  
  49. if __name__ == '__main__':
  50.     executor.start_polling(dp)
  51. ###############################################################################################################
  52.  
  53. #Этот bot lininux
  54.  
  55. ##### Выполните команду
  56. touch /etc/systemd/system/telegram-bot.service
  57. chmod 664 /etc/systemd/system/telegram-bot.service
  58. dnf instal mc
  59.  
  60. ############## Эти команды на перезапуск сервиса #############
  61. systemctl start telegram-bot.service
  62. systemctl enable telegram-bot.service
  63. systemctl status telegram-bot.service
  64. systemctl куыефке telegram-bot.service
  65.  
  66. ############# Выполните это
  67. mcedit etc/systemd/system/telegram-bot.service
  68. ############ И вставьте текст
  69. [Unit]
  70. Description=Telegram bot
  71. After=network.target
  72.  
  73. [Service]
  74. Type=simple
  75. User= root
  76. ExecStart=/home/load/bot.sh (в этом файле тогда надо прописать полный путь до bot.py)
  77.  
  78. [Install]
  79. WantedBy=multi-user.target
  80. #####################################################################
  81.  
  82. #Этот файл config.py
  83. TOKEN = '1791281450:AAHcMZ4SqzchSKgI4iAyDrVOPJhFdb2vCLA'
  84. ##############################################################
  85.  
  86. #Этот файл bot.sh
  87. #!/bin/bash
  88. python3.9 /home/bot/bot.py
  89. #############################################################
  90.  
  91. #Этот файл bot.py
  92. ### Работает на python3.9
  93. import os
  94. import subprocess
  95. import sys
  96. import shlex
  97. import datetime
  98. from subprocess import Popen, PIPE
  99. from aiogram import Bot, types
  100. from aiogram.dispatcher import Dispatcher
  101. from aiogram.utils import executor
  102. import subprocess
  103. from config import TOKEN
  104.  
  105. bot = Bot(token=TOKEN)
  106. dp = Dispatcher(bot)
  107.  
  108. def run_command(command):
  109.     process = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE)
  110.     global textoutput
  111.     textoutput = ''
  112.     while True:
  113.         global output
  114.         output = process.stdout.readline()
  115.         output = output.decode('utf8')
  116.         if output == '' and process.poll() is not None:
  117.             break
  118.         if output:
  119.             print (output.strip())
  120.         textoutput = textoutput + '\n' + output.strip()
  121.     rc = process.poll()
  122.     return rc
  123.  
  124. @dp.message_handler(commands=['start'])
  125. async def process_start_command(message: types.Message):
  126.     await message.reply("Привет!\nМой список команд ограничен твоей фантазией\n/help\n/ifconfig\n/ip\n/df\n/ping!")
  127.  
  128. @dp.message_handler(commands=['ifconfig'])
  129. async def ifconfig(message: types.Message):
  130.     answer_message = run_command("ifconfig")
  131.     await message.answer(textoutput)
  132.  
  133. @dp.message_handler(commands=['ip'])
  134. async def ip(message: types.Message):
  135.     answer_message = run_command("ip addr show")
  136.     await message.answer(textoutput)
  137.  
  138. @dp.message_handler(commands=['df'])
  139. async def df(message: types.Message):
  140.     answer_message = run_command ("df -h" )
  141.     await message.answer(textoutput)
  142.  
  143. @dp.message_handler(commands=['ping'])
  144. async def ping(message: types.Message):
  145.     answer_message = run_command ("ping 8.8.8.8 -c 4" )
  146.     await message.answer(textoutput)
  147.  
  148. if __name__ == '__main__':
  149.     executor.start_polling(dp)
  150.  
Add Comment
Please, Sign In to add comment