Merge stdout and stderr for shell

This commit is contained in:
blank X 2021-08-18 10:53:23 +07:00
parent 934f4f924e
commit feff9c4d8f
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
1 changed files with 3 additions and 7 deletions

View File

@ -16,21 +16,17 @@ async def shell(client, message):
command = match.group(1) command = match.group(1)
stdin = match.group(2) stdin = match.group(2)
reply = await message.reply_text('Executing...') reply = await message.reply_text('Executing...')
process = await asyncio.create_subprocess_shell(command, stdin=asyncio.subprocess.PIPE if stdin else None, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE) process = await asyncio.create_subprocess_shell(command, stdin=asyncio.subprocess.PIPE if stdin else None, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.STDOUT)
stdout, stderr = await process.communicate(stdin.encode() if stdin else None) stdout, _ = await process.communicate(stdin.encode() if stdin else None)
returncode = process.returncode returncode = process.returncode
text = f'<b>Exit Code:</b> <code>{returncode}</code>\n' text = f'<b>Exit Code:</b> <code>{returncode}</code>\n'
stdout = stdout.decode().replace('\r', '').strip('\n').rstrip() stdout = stdout.decode().replace('\r', '').strip('\n').rstrip()
stderr = stderr.decode().replace('\r', '').strip('\n').rstrip()
if stderr:
text += f'<code>{html.escape(stderr)}</code>\n'
if stdout: if stdout:
text += f'<code>{html.escape(stdout)}</code>' text += f'<code>{html.escape(stdout)}</code>'
# send as a file if it's longer than 4096 bytes # send as a file if it's longer than 4096 bytes
if len(text) > 4096: if len(text) > 4096:
out = stderr.strip() + "\n" + stdout.strip() f = BytesIO(stdout.encode('utf-8'))
f = BytesIO(out.strip().encode('utf-8'))
f.name = "output.txt" f.name = "output.txt"
await reply.delete() await reply.delete()
await message.reply_document(f, caption=f'<b>Exit Code:</b> <code>{returncode}</code>') await message.reply_document(f, caption=f'<b>Exit Code:</b> <code>{returncode}</code>')