2017-07-27 22 views
0

我遇到了使用run操作切換遠程服務器上的用戶後不執行命令的問題。將用戶切換爲Fabric後,不執行命令python

事情我想做的事:

  1. 到遠程主機連接
  2. 切換到用戶B
  3. 運行腳本,用戶B在

代碼片段:

env.hosts = ['[email protected]'] 
def build(): 
    run("sudo su - userB") 
    run("./path/to/run.sh") #<----- this line is not executed 

當運行python腳本時,th Ë出色我得到的是:

[[email protected]]執行任務 '構建'

[[email protected]]運行:sudo的蘇 - 用戶B

[用戶A @你好。 COM]爲 '用戶A' 登錄密碼:

[[email protected]]出:[須藤]密碼用戶A:

[[email protected]]出:[[email protected]〜] $

PS:我可以輸入Login和sudo的密碼。

回答

0

每個結構命令的上下文都是相互獨立的。所以你的run("./path/to/run.sh")仍然被userA調用。

您需要與with共享環境背景:

def build(): 
    with settings(user='userB',password='foobar'): 
    run("./path/to/run.sh") 

http://docs.fabfile.org/en/latest/api/core/context_managers.html#fabric.context_managers.settings

+0

這無濟於事。我得到了'/ bin/bash:./path/to/run.sh:權限被拒絕。它似乎不知道「sudo su - userB」。 – Way