I am trying to use fabric to automate some administrative work that I am doing on a couple of servers. The general flow is the following:
sudo su -
to become root (providing local user password again)Unfortunately using run('sudo su -')
blocks execution of the scripts and allows user input. When I type exit
or Ctrl+D
the scipt resumes, but without root privileges.
I have seen a similar problem in Switching user in Fabric but I am restricted to sudo su -
because I am not allowed to change the /etc/sudoers
file which contains the following line:
localuser ALL = /usr/bin/su -
I browsed the source of fabric trying to find a workaround but with no success.
Having faced the same problem as yours, (only sudo su - user
allowed by admin, sudo -u user -c cmd
not allowed), here's my working solution with fabric
:
from ilogue.fexpect import expect, expecting, run
def sudosu(user, cmd):
cmd += ' ;exit'
prompts = []
prompts += expect('bash', cmd)
prompts += expect('assword:', env.password)
with expecting(prompts):
run('sudo su - ' + user)
def host_type():
sudosu('root', 'uname -s')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With