From ca06d6dc79d2352262378fd97629437611fe09a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E7=BF=94?= Date: Wed, 22 Sep 2021 19:13:27 +0800 Subject: [PATCH] fix : append something to a file when sudo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. I met this error INFO:paramiko.transport:Authentication (password) successful! bash: /root/.ssh/authorized_keys: ζƒι™δΈε€Ÿ ERROR:root:Encountered a bad command exit code! Command: "sudo -S -p '[sudo] password: ' echo 'ssh-rsa a_very_long_string root@s1' >> /root/.ssh/authorized_keys" Exit code: 1 2. when I excute the follow command manually, it passed. sudo -S -p '[sudo] password: ' bash -c "echo 'ssh-rsa a_very_long_string root@s1' >> /root/.ssh/authorized_keys " 3. the reason is that `` >> /root/.ssh/authorized_keys`` doesn't have sudo priveledge. So we need ``bash -c "a_long_command" `` to wrap them up. --- patchwork/files.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/patchwork/files.py b/patchwork/files.py index 1e68512..90423e6 100644 --- a/patchwork/files.py +++ b/patchwork/files.py @@ -127,7 +127,8 @@ def append(c, runner, filename, text, partial=False, escape=True): ): continue line = line.replace("'", r"'\\''") if escape else line - runner("echo '{}' >> {}".format(line, filename)) + # runner("echo '{}' >> {}".format(line, filename)) + runner('''bash -c "echo '{}' >> {}" '''.format(line, filename)) def _escape_for_regex(text):