Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
anasty17
GitHub Repository: anasty17/mirror-leech-telegram-bot
Path: blob/master/bot/modules/shell.py
1619 views
1
from io import BytesIO
2
3
from .. import LOGGER
4
from ..helper.ext_utils.bot_utils import cmd_exec, new_task
5
from ..helper.telegram_helper.message_utils import send_message, send_file
6
7
8
@new_task
9
async def run_shell(_, message):
10
cmd = message.text.split(maxsplit=1)
11
if len(cmd) == 1:
12
await send_message(message, "No command to execute was given.")
13
return
14
cmd = cmd[1]
15
stdout, stderr, _ = await cmd_exec(cmd, shell=True)
16
reply = ""
17
if len(stdout) != 0:
18
reply += f"*Stdout*\n<code>{stdout}</code>\n"
19
LOGGER.info(f"Shell - {cmd} - {stdout}")
20
if len(stderr) != 0:
21
reply += f"*Stderr*\n<code>{stderr}</code>"
22
LOGGER.error(f"Shell - {cmd} - {stderr}")
23
if len(reply) > 3000:
24
with BytesIO(str.encode(reply)) as out_file:
25
out_file.name = "shell_output.txt"
26
await send_file(message, out_file)
27
elif len(reply) != 0:
28
await send_message(message, reply)
29
else:
30
await send_message(message, "No Reply")
31
32