Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[consutil] replace shell=True #2725

Merged
merged 2 commits into from
Jun 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions consutil/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import click
from sonic_py_common import device_info
from sonic_py_common.general import getstatusoutput_noshell_pipe

ERR_DISABLE = 1
ERR_CMD = 2
Expand Down Expand Up @@ -199,7 +200,7 @@ def clear_session(self):
try:
if not self._session:
pid = self.session_pid
cmd = "sudo kill -SIGTERM " + pid
cmd = ['sudo', 'kill', '-SIGTERM', str(pid)]
SysInfoProvider.run_command(cmd)
else:
self._session.close()
Expand Down Expand Up @@ -276,7 +277,7 @@ def init_device_prefix():
@staticmethod
def list_console_ttys():
"""Lists all console tty devices"""
cmd = "ls " + SysInfoProvider.DEVICE_PREFIX + "*"
cmd = ["ls", SysInfoProvider.DEVICE_PREFIX + "*"]
output, _ = SysInfoProvider.run_command(cmd, abort=False)
ttys = output.split('\n')
ttys = list([dev for dev in ttys if re.match(SysInfoProvider.DEVICE_PREFIX + r"\d+", dev) != None])
Expand All @@ -285,15 +286,17 @@ def list_console_ttys():
@staticmethod
def list_active_console_processes():
"""Lists all active console session processes"""
cmd = 'ps -eo pid,lstart,cmd | grep -E "(mini|pico)com"'
output = SysInfoProvider.run_command(cmd)
cmd0 = ['ps', '-eo', 'pid,lstart,cmd']
cmd1 = ['grep', '-E', "(mini|pico)com"]
output = SysInfoProvider.run_command(cmd0, cmd1)
return SysInfoProvider._parse_processes_info(output)

@staticmethod
def get_active_console_process_info(pid):
"""Gets active console process information by PID"""
cmd = 'ps -p {} -o pid,lstart,cmd | grep -E "(mini|pico)com"'.format(pid)
output = SysInfoProvider.run_command(cmd)
cmd0 = ['ps', '-p', str(pid), '-o', 'pid,lstart,cmd']
cmd1 = ['grep', '-E', "(mini|pico)com"]
output = SysInfoProvider.run_command(cmd0, cmd1)
processes = SysInfoProvider._parse_processes_info(output)
if len(list(processes.keys())) == 1:
return (list(processes.keys())[0],) + list(processes.values())[0]
Expand Down Expand Up @@ -325,15 +328,12 @@ def _parse_processes_info(output):
return console_processes

@staticmethod
def run_command(cmd, abort=True):
"""runs command, exit if stderr is written to and abort argument is ture, returns stdout, stderr otherwise"""
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, text=True)
output = proc.stdout.read()
error = proc.stderr.read()
if abort and error != "":
click.echo("Command resulted in error: {}".format(error))
def run_command(*args, abort=True):
exitcodes, output = getstatusoutput_noshell_pipe(*args)
if abort and any(exitcodes) and output != '':
click.echo("Command resulted in error: {}".format(output))
sys.exit(ERR_CMD)
return output if abort else (output, error)
return output if abort else (output, output)

class DbUtils(object):
def __init__(self, db):
Expand Down