mipadi mipadi - 9 months ago 58
Python Question

Can I prevent fabric from prompting me for a sudo password?

I am using Fabric to run commands on a remote server. The user with which I connect on that server has some sudo privileges, and does not require a password to use these privileges. When SSH'ing into the server, I can run

sudo blah
and the command executes without prompting for a password. When I try to run the same command via Fabric's
function, I get prompted for a password. This is because Fabric builds a command in the following manner when using

sudo -S -p <sudo_prompt> /bin/bash -l -c "<command>"

Obviously, my user does not have permission to execute
without a password.

I've worked around the problem by using
run("sudo blah")
instead of
, but I wondered if there is a better solution. Is there a workaround for this issue?

Answer Source

Try passing shell=False to sudo. That way /bin/bash won't be added to the sudo command. sudo('some_command', shell=False)

From line 503 of fabric/operations.py:

if (not env.use_shell) or (not shell):
    real_command = "%s %s" % (sudo_prefix, _shell_escape(command))

the else block looks like this:

                                             # V-- here's where /bin/bash is added
real_command = '%s %s "%s"' % (sudo_prefix, env.shell,
    _shell_escape(cwd + command))