在 fabric 2 中如何 运行 不同的用户?
In fabric 2 how to run a diffent user?
我希望使用 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:
我该怎么做才对?
有几种方法,您可以指定用户 kwarg,或修改配置:
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
我希望使用 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:
我该怎么做才对?
有几种方法,您可以指定用户 kwarg,或修改配置:
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