sycgram/plugins/shell.py

41 lines
1.2 KiB
Python
Raw Permalink Normal View History

2022-04-06 14:39:27 +00:00
import asyncio
from getpass import getuser
from io import BytesIO
from platform import node
from core import command
from pyrogram import Client
from pyrogram.types import Message
2022-04-09 05:11:35 +00:00
from tools.helpers import Parameters, basher, delete_this, show_cmd_tip, show_exception
2022-04-06 14:39:27 +00:00
@Client.on_message(command("sh"))
async def shell(_: Client, msg: Message):
"""执行shell脚本"""
cmd, _input = Parameters.get(msg)
if not _input:
2022-04-09 05:11:35 +00:00
return await show_cmd_tip(msg, cmd)
2022-04-06 14:39:27 +00:00
try:
res = await basher(_input, timeout=30)
except asyncio.exceptions.TimeoutError:
2022-04-09 05:11:35 +00:00
return await show_exception(msg, "Connection Timeout")
2022-04-06 14:39:27 +00:00
2022-04-09 05:11:35 +00:00
_output: str = res.get('output') if not res.get(
'error') else res.get('error')
2022-04-06 14:39:27 +00:00
header = f"**{getuser()}@{node()}**\n"
all_bytes = len(header.encode() + _input.encode() + _output.encode())
if all_bytes >= 2048:
await delete_this(msg)
2022-04-09 05:11:35 +00:00
return await msg.reply_document(
2022-04-06 14:39:27 +00:00
document=BytesIO(_output.encode()),
caption=f"{header}> # `{_input}`",
file_name="output.log",
parse_mode='md'
)
await msg.edit_text(
f"{header}> # `{_input}`\n```{_output.strip()}```",
parse_mode='md'
)