Running root scripts

I am trying to use fabric to automate some administrative work that I do on multiple servers. The general flow is as follows:

  • SSH with local user
  • run: sudo su -to become root (again providing the local user password).
  • Do the work as root :)

Unfortunately, use run('sudo su -')blocks script execution and allows user input. When I type exitor Ctrl+D, scipt resumes, but without root privileges.

I saw a similar problem in Switching a user in Fabric , but I am limited sudo su -because I am not allowed to modify the file /etc/sudoersthat contains the following line:

localuser ALL = /usr/bin/su -

I looked at the source of the fabric, trying to find a workaround, but without success.

+5
source share
3 answers

faced with the same problem as yours (only sudo su - userallowed by the administrator, sudo -u user -c cmdnot allowed), here is 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')
+4
source

There are several solutions to your problem. First, you want to run commands with sudo. You can use the cloth method sudoinstead run, which runs a shell command on a remote host, with superuser privileges( sudo ref ).

For example, these commands are executed using sudo:

sudo("~/install_script.py")
sudo("mkdir /var/www/new_docroot", user="www-data")
sudo("ls /home/jdoe", user=1001)
result = sudo("ls /tmp/")

, ( sudo ed). Fabric (ref). , prefix settings.

:

with settings(user='root'):
    run('do something')
    run('do another thing')

, root root. .

+1

There is one solution for the next task Sorry, user localuser is not allowed to execute '/usr/bin/su - -c /bin/bash -l -c pwd' as root on hostname. you can try sudo('mkdir ~/test_fabric',shell=False). Using option shellto avoid bash param -l.

0
source

All Articles