From c8a74f008486ad44954c7bdad5b96494caa09197 Mon Sep 17 00:00:00 2001 From: Victor Moene Date: Fri, 20 Dec 2024 11:44:37 +0100 Subject: [PATCH] Added sudo skipping when UID is zero Ticket: CFE-4464 Signed-off-by: Victor Moene --- cf_remote/ssh.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/cf_remote/ssh.py b/cf_remote/ssh.py index 8b9a047..7920bbc 100644 --- a/cf_remote/ssh.py +++ b/cf_remote/ssh.py @@ -131,6 +131,7 @@ def connect(host, users=None): c.ssh_user = user c.ssh_host = host c.run("whoami", hide=True) + c.has_sudo = c.run("echo $UID", hide=True).stdout.strip() != '0' return c except aramid.ExecutionError: continue @@ -198,9 +199,12 @@ def ssh_sudo(connection, cmd, errors=False): assert connection log.debug("Running(sudo) over SSH: '%s'" % cmd) - escaped = cmd.replace('"', r"\"") - sudo_cmd = 'sudo bash -c "%s"' % escaped - result = connection.run(sudo_cmd, hide=True) + if connection.has_sudo: + escaped = cmd.replace('"', r"\"") + sudo_cmd = 'sudo bash -c "%s"' % escaped + result = connection.run(sudo_cmd, hide=True) + else : + result = connection.run(cmd, hide=True) if result.retcode == 0: output = result.stdout.strip("\n") log.debug("'%s' -> '%s'" % (cmd, output))