Skip to content

Instantly share code, notes, and snippets.

@yureative
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save yureative/1a74977f37c31e89e603 to your computer and use it in GitHub Desktop.

Select an option

Save yureative/1a74977f37c31e89e603 to your computer and use it in GitHub Desktop.
The sample code for executing a command without `sudo` but `su -c`.
import os
from fabric.api import env, puts, sudo, settings
from fabric.colors import cyan
DOMAIN_LIST = '%s/.ssh/config' % os.environ['HOME']
env.use_ssh_config = True
env.disable_known_hosts = True
env.password = 'xxxxxx'
def set_all_hosts():
with open(DOMAIN_LIST) as f:
all_hosts = map(lambda y: y.split()[1],
filter(lambda x: x.startswith('Host'), f.read().splitlines()))
env.hosts = all_hosts
puts(cyan('env.hosts = %s' % all_hosts))
def sudo_command(cmd):
with settings(sudo_prefix='su -c ', sudo_prompt='Password: '):
sudo(cmd)
@yureative

Copy link
Copy Markdown
Author
$ fab -H host1 sudo_command:'head /etc/shadow'
or
$ fab set_all_hosts sudo_command:'head /etc/shadow'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment