2010-04-16 36 views
13

這裏和這個question有關,但是稍微有點扭曲:不是隻傳遞'yes'或'no',我需要Fabric將任意字符串傳遞給遠程shell。Python面料:如何處理任意遠程shell提示輸入?

例如,如果遠程shell提示'你的名字是什麼?'那麼我需要給它「首先,最後」。

說明:我知道我說的是任意輸入,但我確實是trying to use it for the SSH key passwd prompt when I try to do a git pull

更新#1:得到了傑夫Forcier響應@bitprophet

+0

可能重複[如何回答以自動提示與python結構?](http://stackoverflow.com/questions/10479078/how-to-answer-to-prompts-automatically-與蟒蛇織物) – Breedly 2017-02-05 15:11:57

回答

4

Fabric 1.0最終支持與遠程服務器的交互。詳情請參閱this page

2

也許考慮pexpect

+0

嗯,非常有趣,看起來像它可以工作。我會放棄它。謝謝你的提示! :) – Jay 2010-04-16 20:33:19

1

我建立了一個名爲project_name/.git的git源代碼庫。

ssh to the server, (entering ssh passwords or passphrases as I go) 
    mkdir project_name 
    cd project_name 
    git init 
    touch fabfile.py 
    git add fabfile.py 
    git commit -a -m "almost empty" 
    git checkout -b web 

我離開分支網站簽出。回到本地機器。

我從服務器通過克隆拉,並添加了我的項目目錄在本地回購分公司主分支。儘管這些步驟也可以自動化,但我並不想使用結構,只是設置了一些東西,而且沒有一個需要另一個SSH密碼。

cd /path/to/project_name/.. 
    git clone ssh://[email protected]_server.com/var/web/project_name/.git 
    cd project_name 
    gvim fabfile.py 
    git add fabfile.py 
    git commit -a -m "fabfile edits" 

現在我開始使用fabric。下面是我的fabfile摘錄管理Git標籤 和分支:

#Usage: fab committag brpush | fab committag push | fab push | fab tag 
def committag(): 
    """commit chgs, tag new commit, push tags to server.""" 
    prompt('commit descr: ', 'COM_MSG', default='new stuff') 
    prompt('commit name: ', 'COM_NAME', default='0.0.1') 
    local('git commit -a -m "%(COM_MSG)s"' % env) 
    local('sleep 1') 
    local('git tag -u "John Griessen" -m "%(COM_MSG)s" %(COM_NAME)s' % env) 
    local('sleep 1') 
    local('git push origin --tags') #pushes local tags 

def brpush(): 
    """create a new branch, default COM_NAME, then push to server.""" 
    prompt('new branch name: ', 'BR_NAME', default= '%(COM_NAME)s' % env) 
    local('git checkout -b %(BR_NAME)s' % env) 
    local('sleep 2') 
    local('git checkout master') 
    local('git push origin --tags') #pushes local tags 
    local('git push --all origin') #pushes local master and branches 

def push(): 
    """Push existing tags and changes to server.""" 
    local('git push origin --tags') #pushes local tags 
    local('git push --all origin') #pushes local master and branches 


def tag(): #Call this from committag() 
    """create a gpg signed tag on the local git repo tag from prompted name .""" 
    prompt('tag descr: ', 'TAG_MSG', default='0.0.1') 
    prompt('tag name: ', 'TAG_NAME', default='0.0.1') 
    local('git tag -u "John Griessen" -m "%(TAG_MSG)s" %(TAG_NAME)s' % env) 

使用上述fabfile DEFS,我只是做一些改變我的項目目錄, 想着它們的apporpriate消息,並做:

$fab committag 

我在服務器上標記和更新了更改。或者:

$fab committag brpush 

我創建了一個新分支並更新了服務器。

1

跳過主機驗證提示的一種方法是:

run('ssh-keyscan github.com > ~/.ssh/known_hosts') 

而且,我使用​​安裝deploy keys:

run('ssh-keygen -q -t rsa -f /home/%(user)s/.ssh/id_rsa -N ""' % env) 
key = run('cat /home/%(user)s/.ssh/id_rsa.pub' % env) 
gh.repos.addDeployKey(repo, env.host, key) 
5

我已經提出了這個功能的API面料郵件列表, ,最後寫了一些東西:

from fexpect import expect, expecting, run 

prompts = [] 
prompts += expect('What is your name?','John') 
prompts += expect('Where do you live?','New York') 

with expecting(prompts): 
    run('command') 

查看我的博文對expecting prompts in fabric with fexpect

+0

博客文章鏈接不再有效,「ilogue.com正在出售中......」這個博客是否可以在其他地方找到?或者我們應該滿足於存檔頁面? http://web.archive.org/web/20140624141333/http://ilogue.com/jasper/blog/fexpect--dealing-with-prompts-in-fabric-with-pexpect/ – FooF 2015-12-01 05:47:12

相關問題