如何强制fabric使用env.password设置

时间:2015-11-26 04:00:00

标签: python fabric

我有一个结构文件,我在其文档中读到的时候设置了env.password:

from fabric.api import *


env.password = "mypassword"

print env.user
print env.password
def update():
    local("sudo apt-get update")

然而,当我运行该文件时,它仍然提示我输入密码:

cchilders@cchilders-Dell-Precision-M3800:~$ fab -f fab_setup_new_linux_box.py update
cchilders
mypassword
[localhost] local: sudo apt-get update
[sudo] password for cchilders: 

我认为这是设置env.password的想法?当我使用sudo或run时,它说:

"找不到主机。请为连接指定(单个)主机字符串:"

如何使用我设置的密码运行它?谢谢

1 个答案:

答案 0 :(得分:1)

我得到了它的工作。它不漂亮。

from fabric.api import *


env.password = "mypassword"

print env.user
print env.password
def update():
    local("echo {} | sudo -S apt-get update".format(env.password))

是的,我也不喜欢它。这是输出:(是的,我在我的虚拟机上创建了你的用户)

$ fab update
cchilders
mypassword
[localhost] local: echo mypassword | sudo -S apt-get update
Hit http://ppa.launchpad.net precise InRelease
Hit http://ppa.launchpad.net precise/main Sources                                                                                   
Hit http://ppa.launchpad.net precise/main amd64 Packages                                                    
Hit http://ppa.launchpad.net precise/main i386 Packages              
Hit http://ppa.launchpad.net precise/main TranslationIndex           
Hit http://security.ubuntu.com precise-security InRelease            
Hit http://ppa.launchpad.net precise/main Translation-en                
Ign http://us.archive.ubuntu.com precise InRelease
Hit http://us.archive.ubuntu.com precise-updates InRelease
Hit http://us.archive.ubuntu.com precise-backports InRelease
Get:1 http://security.ubuntu.com precise-security/main Sources [136 kB] 
Get:2 http://us.archive.ubuntu.com precise Release.gpg [198 B]
Get:3 http://us.archive.ubuntu.com precise-updates/main Sources [494 kB]
Get:4 http://security.ubuntu.com precise-security/restricted Sources [4,476 B]
Get:5 http://security.ubuntu.com precise-security/universe Sources [43.8 kB] 
Get:6 http://security.ubuntu.com precise-security/multiverse Sources [2,198 B]        
Get:7 http://security.ubuntu.com precise-security/main amd64 Packages [563 kB]
...