我希望使用fabric 2在我的服务器上以与我登录的用户不同的方式进行操作。
from fabric import Connection, Config
HOST = 'myserver'
sudo_pass = 'mypassword'
config = Config(overrides={'sudo': {'password': sudo_pass}})
c = Connection(HOST, config=config)
c.run('whoami')
c.sudo('whoami')
with c.sudo('su ceres'):
c.run('whoami')
myname
[sudo] password: root
[sudo] password:
我该怎么做?
答案 0 :(得分:3)
有两种方法,您可以指定用户kwarg,也可以修改config:
from fabric import Connection, Config
HOST = 'myserver'
sudo_user = 'ceres'
sudo_pass = 'mypassword'
config = Config(
overrides={
'sudo': {
'user': sudo_user,
'password': sudo_pass
}
}
)
c = Connection(HOST, config=config)
c.run('whoami')
c.sudo('whoami')
c.sudo('whoami', user='root')
看起来像这样:
myname
[sudo] password: ceres
[sudo] password: root