擅长:python、mysql、java
<p>你的问题有几种解决方法。首先,要使用<code>sudo</code>运行命令。您可以使用fabric方法<code>sudo</code>,而不是<code>run</code>,即<code>runs a shell command on a remote host, with superuser privileges</code>(<a href="http://docs.fabfile.org/en/1.4.2/api/core/operations.html?highlight=sudo#fabric.operations.sudo" rel="nofollow">sudo ref</a>)。</p>
<p>例如,这些命令使用<code>sudo</code>执行:</p>
<pre><code>sudo("~/install_script.py")
sudo("mkdir /var/www/new_docroot", user="www-data")
sudo("ls /home/jdoe", user=1001)
result = sudo("ls /tmp/")
</code></pre>
<p>另一个想法是要包装一组命令(需要<code>sudo</code>ed)。
您可以使用Fabric上下文管理器(<a href="http://docs.fabfile.org/en/1.4.2/api/core/context_managers.html?highlight=cd#fabric.context_managers.cd" rel="nofollow">ref</a>)来实现这一点。特别是,您可以使用<code>prefix</code>或<code>settings</code>。</p>
<p>例如:</p>
<pre><code>with settings(user='root'):
run('do something')
run('do another thing')
</code></pre>
<p>将询问您一次根密码,然后作为根执行命令。
您可以选择存储密码的设置。</p>