From ab7abf96a6832a227fe2218bb191cc2d1fc73030 Mon Sep 17 00:00:00 2001 From: "Jose M. Guisado" Date: Mon, 15 Nov 2021 12:08:43 +0100 Subject: #1065 linux: add shell run operation - Executed script runs with same privilege as ogClient process. - Uses subprocess.run instead of subprocess.Popen, it's a bit simpler. We can't specify executable, though. Shouldn't need so in Linux mode. - Uses shell=True, keep in mind security considerations listed at: https://docs.python.org/3/library/subprocess.html#security-considerations (shlex.quote can be used for unix shells) --- src/linux/ogOperations.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/linux/ogOperations.py b/src/linux/ogOperations.py index 3970d41..f47b7a8 100644 --- a/src/linux/ogOperations.py +++ b/src/linux/ogOperations.py @@ -7,10 +7,13 @@ # (at your option) any later version. import os -from pystray import Icon, Menu, MenuItem +import subprocess +from subprocess import CalledProcessError import multiprocessing as mp from multiprocessing import Process + from PIL import Image, ImageDraw +from pystray import Icon, Menu, MenuItem from src.ogRest import ThreadState @@ -74,7 +77,21 @@ class OgLinuxOperations: os.system('systemctl reboot') def shellrun(self, request, ogRest): - raise NotImplementedError + cmd = request.getrun() + try: + result = subprocess.run(cmd, + shell=True, + stdin=subprocess.DEVNULL, + capture_output=True, + text=True, + check=True) + except CalledProcessError as error: + if error.stderr: + return error.stderr + if error.stdout: + return error.stdout + return "{Non zero exit code and empty output}" + return result.stdout def session(self, request, ogRest): raise NotImplementedError -- cgit v1.2.3-18-g5258