每次我执行sudo命令时,python fabric都会提示输入密码

每次我执行sudo命令时,python fabric都会提示输入密码,python,fabric,Python,Fabric,下面是我一直在编写的在远程服务器上安装软件包的小代码。代码工作正常,软件包正在安装中,但安装完成后,系统会提示我输入密码。你知道这是什么原因吗 import re import os import sys from datetime import datetime import git from fabric.api import run, local, cd, env from fabric.contrib.files import exists, append from fabric.o

下面是我一直在编写的在远程服务器上安装软件包的小代码。代码工作正常,软件包正在安装中,但安装完成后,系统会提示我输入密码。你知道这是什么原因吗

import re
import os
import sys
from datetime import datetime

import git

from fabric.api import run, local, cd, env
from fabric.contrib.files import exists, append
from fabric.operations import put, sudo


class WebAppConf():
    APPLICATION_DIR = '/home/xxx/webapps'
    APPLICATION_USER = '' # mazban
    APPLICATION_ROOT = ''
    APPLICATION_TEMPLATE_SETTINGS = 'com_work'  # see config/settings/templates

BASE_DIR = os.path.abspath(os.path.dirname(__file__))

###########################################################################
# CONFIGURATION FOR DEVELOPMENT, STAGING, TESTING AND PRODUCTION SETTINGS - START     #
###########################################################################

def test():
    env.hosts = env.hosts or ['192.168.3.139', ]
    env.user = 'xxx'
    env.password = "xxx"
    env.warn_only = True
    env.no_keys = True
    # App variables 
    WebAppConf.APPLICATION_DIR = '/home/xxx/webapps' # application physical location
    WebAppConf.APPLICATION_USER = env.user
    WebAppConf.APPLICATION_ROOT = env.user
    WebAppConf.APPLICATION_TEMPLATE_SETTINGS = 'testing'


def staging():
    env.hosts = env.hosts or ['xxx-staging.com', ]
    env.user = 'xxx'
    env.password = "xx"
    env.warn_only = True
    env.no_keys = True

def production():
    """
    Production will deploay on one or many frontend's
        for peroduction deployment no password will be used and only ssh keys
    """
    env.hosts = env.hosts or ['xxx.com', ]
    env.user = APPLICATION_USER
    env.password = "xx"
    env.warn_only = True
    env.no_keys = True

##########################################################################
# CONFIGURATION FOR DEVELOPMENT, STAGING, TESTING AND PRODUCTION SETTINGS - END      #
##########################################################################


def firstrun():
    ## if no env is selected it will fail
    if not env.hosts:
        print 'Error: must use environment (e.g fab prod %s)' % 'firstrun'
        exit()

    # validate the OS distribution and version
    LINUX_DISTRIBUTION = ''
    if run('cat /etc/*-release|grep DISTRIB_ID').upper().__contains__('UBUNTU'):
        LINUX_DISTRIBUTION = 'UBUNTU'
        print 'Ubuntu Linux'
    elif run('cat /etc/*-release|grep DISTRIB_ID').upper().__contains__('DEBIAN'):
        LINUX_DISTRIBUTION = 'DEBIAN'
        print 'Debian Linux'
    else:
        print 'Exiting (Cannot recognize linux distribution).'
        exit()

    # Linux version
    LINUX_VERSION = run('cat /etc/*-release|grep DISTRIB_RELEASE')
    print 'Linux version %s' % LINUX_VERSION.replace('DISTRIB_RELEASE=', '')

    # GET PYTHON VERSIONS
    version = run("python2.6 -V").split()[1]
    if version[0]=='2' and version[2]=='6' and version[4]=='6':
        # version installed matches the requirements
        print 'Python version is compatible with this application (2.6.6).'
    else:
        print 'Python version is not compatible with this application %s' % version[0]+'.'+version[2]+'.'+version[4]
        _sudo('apt-get install python2.6')
        version = run("python2.6 -V").split()[1]
        if version[0]=='2' and version[2]=='6' and version[4]=='6':
            print 'python 2.6.6 installed successfully.'
        else:
            print 'cannot install python 2.6.6'
            exit()

    if not exists('/usr/bin/pip'):
        print "installing python-pip package"
        _sudo('apt-get install python-pip')

    if not exists('/usr/local/bin/virtualenv'):
        print 'installing virtualenv'
        _sudo('pip install -E /usr/bin/python2.6 virtualenv')


    # application directory check
    MAKE_APPLICATION_DIR = False
    if not exists(WebAppConf.APPLICATION_DIR):
        MAKE_APPLICATION_DIR = True
        print '%s does not exist.' % WebAppConf.APPLICATION_DIR
        run('mkdir -p %s' % WebAppConf.APPLICATION_DIR)
        print '%s application directory is created successfully.' % WebAppConf.APPLICATION_DIR

    # change to application root directory and start preparing for applications
    #with cd(WebAppConf.APPLICATION_DIR):
    return
执行函数时的输出

mo@ubuntu:~/Projects/mazban/mazban$ fab test firstrun
[192.168.3.139] Executing task 'firstrun'
[192.168.3.139] run: cat /etc/*-release|grep DISTRIB_ID
[192.168.3.139] out: DISTRIB_ID=Ubuntu
[192.168.3.139] out: 
Ubuntu Linux
[192.168.3.139] run: cat /etc/*-release|grep DISTRIB_RELEASE
[192.168.3.139] out: DISTRIB_RELEASE=11.04
[192.168.3.139] out: 
Linux version 11.04
[192.168.3.139] run: python2.6 -V
[192.168.3.139] out: /bin/bash: python2.6: command not found
[192.168.3.139] out: 

Warning: run() encountered an error (return code 127) while executing 'python2.6 -V'

Python version is not compatible with this application p.t.o
[192.168.3.139] sudo: apt-get install python2.6
[192.168.3.139] out: sudo password:
[192.168.3.139] out: Reading package lists... Done
[192.168.3.139] out: Building dependency tree       
[192.168.3.139] out: Reading state information... Done
[192.168.3.139] out: The following extra packages will be installed:
[192.168.3.139] out:   python2.6-minimal
[192.168.3.139] out: Suggested packages:
[192.168.3.139] out:   python2.6-doc python2.6-profiler
[192.168.3.139] out: The following NEW packages will be installed:
[192.168.3.139] out:   python2.6 python2.6-minimal
[192.168.3.139] out: 0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
[192.168.3.139] out: Need to get 3,743 kB of archives.
[192.168.3.139] out: After this operation, 14.3 MB of additional disk space will be used.
[192.168.3.139] out: Do you want to continue [Y/n]? Y
[192.168.3.139] out: Get:1 http://us.archive.ubuntu.com/ubuntu/ natty/main python2.6-minimal i386 2.6.6-6ubuntu7 [1,386 kB]
[192.168.3.139] out: Get:2 http://us.archive.ubuntu.com/ubuntu/ natty/main python2.6 i386 2.6.6-6ubuntu7 [2,357 kB]
[192.168.3.139] out: 53% [2 python2.6 602 kB/2,357 kB 25%]                                                                                                                        [192.168.3.139] out: 58% [2 python2.6 797 kB/2,357 kB 33%]                                                                                                                        [192.168.3.139] out: 63% [2 python2.6 1,001 kB/2,357 kB 42%]                                                                                                                      [192.168.3.139] out: 69% [2 python2.6 1,208 kB/2,357 kB 51%]                                                                                                                      [192.168.3.139] out: 75% [2 python2.6 1,425 kB/2,357 kB 60%]                                                                                                                      [192.168.3.139] out: 81% [2 python2.6 1,651 kB/2,357 kB 70%]                                                                                                                      [192.168.3.139] out: 87% [2 python2.6 1,890 kB/2,357 kB 80%]                                                                                                                      [192.168.3.139] out: 94% [2 python2.6 2,141 kB/2,357 kB 90%]                                                                                                                      [192.168.3.139] out: 100% [Working]                                                                                                                                               [192.168.3.139] out:                                                                                                                                                              [192.168.3.139] out: Fetched 3,743 kB in 10s (371 kB/s)
[192.168.3.139] out: Selecting previously deselected package python2.6-minimal.
[192.168.3.139] out: (Reading database ... 131472 files and directories currently installed.)
[192.168.3.139] out: Unpacking python2.6-minimal (from .../python2.6-minimal_2.6.6-6ubuntu7_i386.deb) ...
[192.168.3.139] out: Selecting previously deselected package python2.6.
[192.168.3.139] out: Unpacking python2.6 (from .../python2.6_2.6.6-6ubuntu7_i386.deb) ...
[192.168.3.139] out: Processing triggers for man-db ...
[192.168.3.139] out: Processing triggers for bamfdaemon ...
[192.168.3.139] out: Rebuilding /usr/share/applications/bamf.index...
[192.168.3.139] out: Processing triggers for desktop-file-utils ...
[192.168.3.139] out: Processing triggers for python-gmenu ...
[192.168.3.139] out: Rebuilding /usr/share/applications/desktop.en_US.utf8.cache...
[192.168.3.139] out: Processing triggers for python-support ...
[192.168.3.139] out: Setting up python2.6-minimal (2.6.6-6ubuntu7) ...
[192.168.3.139] out: Linking and byte-compiling packages for runtime python2.6...
[192.168.3.139] out: Setting up python2.6 (2.6.6-6ubuntu7) ...
[192.168.3.139] out: 
[192.168.3.139] run: python2.6 -V
[192.168.3.139] Login password: 

我的经验有限,但我相信既然你是“sudo”安装py2.6,你就需要是“sudo”来运行它,或者至少是在正确的组中(在Ubuntu上是“wheel”还是类似的东西?)

我想一个简单的测试是,改变这个:

 _sudo('apt-get install python2.6')
 version = run("python2.6 -V").split()[1]
为此:

 _sudo('apt-get install python2.6')
 version = _sudo("python2.6 -V").split()[1]

&看看是否仍然提示您输入密码。

请添加整个脚本,或者至少在包含所有定义的地方。我刚刚添加了完整脚本,我更新了现有的postAs Auston说。注意run()和sudo()是相同的。如果希望另一端出现提示,请使用sudo()。(因此相反,如果我在sudoers中将用户的命令配置为NOPASSWD,那么当我运行sudo命令时,我不会使用sudo(),而是使用run('sudo blahcommand'))