Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用fabric切换到不同的用户_Python_Deployment_Fabric - Fatal编程技术网

Python 使用fabric切换到不同的用户

Python 使用fabric切换到不同的用户,python,deployment,fabric,Python,Deployment,Fabric,我最近开始研究用于远程部署的结构。我需要切换到一个不同的用户(从我登录的那个用户),但无法找到它。有可能吗?如果有,怎么可能?我的当前用户没有sudo权限 我尝试更改以下环境变量 env.sudo_prefix = "su newUser -c " env.sudo_prompt = "Password:" 但fabric不等待“newUser”的密码输入,因此失败 out: Password: [oldUser@ec2-111-11-111-111.compute-1.amazonaws.

我最近开始研究用于远程部署的结构。我需要切换到一个不同的用户(从我登录的那个用户),但无法找到它。有可能吗?如果有,怎么可能?我的当前用户没有
sudo
权限

我尝试更改以下环境变量

env.sudo_prefix = "su newUser -c "
env.sudo_prompt = "Password:"
但fabric不等待“newUser”的密码输入,因此失败

out: Password: 
[oldUser@ec2-111-11-111-111.compute-1.amazonaws.com] out: su: incorrect password

Fatal error: sudo() received nonzero return code 1 while executing!

Requested: touch x
Executed: su newUser -c  -u "root"  /bin/bash -l -c "cd /home/oldUser/upgrade && touch x"

Aborting.
Disconnecting from oldUser@ec2-111-11-111-111.compute-1.amazonaws.com... done.
更新:


正如J.F.Sebastian所建议的那样,
su newUser-c
可以工作,但它会为每个服务器的每个命令提示密码,这有点违背了自动化的目的。如果您不能使用ssh作为
newuser
,并且不能使用
sudo(command,user='newuser')
,那么Fabric中是否有任何方法可以基于提示传递相同的值(在这种情况下,它总是
密码:

谢谢,有两个陷阱

  • Fabric以惰性方式建立连接,因此我必须在调用su之前建立一个虚拟连接,以避免上下文切换
  • Pwd需要存储在全局范围内,以便可以重用。结构不会将其放在缓存中以用于重写的su命令
  • 以下是最终的结果。它起作用了

    pwd = None
    @hosts('myhost.com')
    def test():
        with cd('/home/oldUser/upgrade'):
            run('ls')  #This is to connect aggressively (instead of lazily)
            global pwd  #Change the scope of pwd
            if pwd is None:
                pwd = getpass.getpass('enter password for newUser')
    
            execute(su, pwd, 'newUser', 'touch x')  
            run ('ls')
            execute(su, pwd, 'newUser', 'rm x') 
            run ('ls')
    
    def su(pwd, user, command):
        with settings(
            password= "%s" % pwd,
            sudo_prefix="su %s -c " % user,
            sudo_prompt="Password:"
            ):
            sudo(command)
    

    你试过sudo(“su newuser”)吗?是的,我试过。问题是oldUser不是sudoer,因此任何sudo命令都会失败,并且[out:oldUser不在sudoers文件中。将报告此事件。]这是尝试的最合理的选择,但不幸的是没有:(它应该在没有任何自定义的情况下工作,即,
    run('su-newuser-c command'))
    按预期工作。您的结构版本是什么?是的,此命令的唯一问题是它会提示每个服务器的每个运行命令输入密码(结构无法将其存储为密码)。这在某种程度上违背了脚本的目的。
    pwd = None
    @hosts('myhost.com')
    def test():
        with cd('/home/oldUser/upgrade'):
            run('ls')  #This is to connect aggressively (instead of lazily)
            global pwd  #Change the scope of pwd
            if pwd is None:
                pwd = getpass.getpass('enter password for newUser')
    
            execute(su, pwd, 'newUser', 'touch x')  
            run ('ls')
            execute(su, pwd, 'newUser', 'rm x') 
            run ('ls')
    
    def su(pwd, user, command):
        with settings(
            password= "%s" % pwd,
            sudo_prefix="su %s -c " % user,
            sudo_prompt="Password:"
            ):
            sudo(command)