2010-04-16 49 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