From a351ea4ba6c3d786a3fbbadc026e9af57b721ddc Mon Sep 17 00:00:00 2001 From: iouAkira Date: Tue, 23 Mar 2021 17:39:50 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=E4=BF=AE=E6=AD=A3=E6=89=A7?= =?UTF-8?q?=E8=A1=8Cshell=E8=84=9A=E6=9C=AC=E8=B6=85=E6=97=B6=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E8=8E=B7=E5=8F=96=E6=A0=87=E5=87=86=E8=BE=93=E5=87=BA?= =?UTF-8?q?=E7=BB=93=E6=9E=9C=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=8C=E9=A1=BA?= =?UTF-8?q?=E5=B8=A6=E5=8A=A0=E5=A4=A7tg=E6=B6=88=E6=81=AF=E8=A1=8C?= =?UTF-8?q?=E6=95=B0=EF=BC=8C=E5=B0=BD=E9=87=8F=E4=B8=8D=E7=94=9F=E6=88=90?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=96=87=E4=BB=B6=E6=96=B9=E4=BE=BF=E9=98=85?= =?UTF-8?q?=E8=AF=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker/bot/jd_bot | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/docker/bot/jd_bot b/docker/bot/jd_bot index 4607ae7e..1c645561 100644 --- a/docker/bot/jd_bot +++ b/docker/bot/jd_bot @@ -713,22 +713,32 @@ def gen_daily_code(update, context): def shcmd(update, context): """ 执行终端命令,超时时间为60,执行耗时或者不退出的指令会报超时异常 - :param update: - :param context: - :return: """ if is_admin(update.message.from_user.id): commands = update.message.text.split() commands.remove('/cmd') if len(commands) > 0: - support_cmd = ["ls", "cp", "mv", "wget", "cat", "sed", "git", "sh", "docker_entrypoint.sh"] + support_cmd = ["echo", "ls", "pwd", "cp", "mv", "ps", "wget", "cat", "sed", "git", "sed", "apk", "sh", + "docker_entrypoint.sh"] if commands[0] in support_cmd: + sp_cmd = ["sh", "docker_entrypoint.sh"] cmd = ' '.join(commands) try: - out_bytes = subprocess.check_output( - cmd, shell=True, timeout=60, stderr=subprocess.STDOUT) - out_text = out_bytes.decode('utf-8') - if len(out_text.split()) > 50: + # 测试发现 subprocess.check_output 执行shell 脚本文件的时候无法正常执行获取返回结果 + # 所以 ["sh", "docker_entrypoint.sh"] 指令换为 subprocess.Popen 方法执行 + out_text = "" + if commands[0] in sp_cmd: + p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + while p.poll() is None: + line = p.stdout.readline() + # logger.info(line.decode('utf-8')) + out_text = out_text + line.decode('utf-8') + else: + out_bytes = subprocess.check_output( + cmd, shell=True, timeout=60, stderr=subprocess.STDOUT) + out_text = out_bytes.decode('utf-8') + + if len(out_text.split('\n')) > 100: msg = context.bot.sendMessage(text='```{}```'.format( helpers.escape_markdown(' ↓↓↓ %s 执行结果超长,请查看log ↓↓↓' % cmd)), chat_id=update.effective_chat.id, @@ -754,7 +764,8 @@ def shcmd(update, context): else: update.message.reply_text( text='```{}```'.format( - helpers.escape_markdown(f' →→→ {commands[0]}指令不在支持命令范围,请输入其他支持的指令{"|".join(support_cmd)} ←←← ')), + helpers.escape_markdown( + f' →→→ {commands[0]}指令不在支持命令范围,请输入其他支持的指令{"|".join(support_cmd)} ←←← ')), parse_mode=ParseMode.MARKDOWN_V2) else: update.message.reply_text(