【问题标题】:Python Fabric: How to handle arbitrary remote shell prompt for input?Python Fabric:如何处理任意远程 shell 提示符以进行输入?
【发布时间】:2011-02-08 15:55:30
【问题描述】:

这与这里的question 相关,但略有不同:我需要 Fabric 将任意字符串传递给远程 shell,而不是只传递“是”或“否”。

例如,如果远程 shell 提示“你叫什么名字?”那么我需要先喂它,最后喂它。

澄清:我知道我说的是任意输入,但我真的是trying to use it for the SSH key passwd prompt when I try to do a git pull

更新 #1:得到 Jeff Forcier @bitprophet 的回复

【问题讨论】:

标签: python django unix shell fabric


【解决方案1】:

我已经在邮件列表中为 fabric 中的这个特性提出了一个 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上查看我的博文

【讨论】:

【解决方案2】:

Fabric 1.0 最终支持与远程服务器的交互。详情请见this page

【讨论】:

    【解决方案3】:

    也许看看pexpect

    【讨论】:

    • 嗯,很有趣,看起来可以工作。我要试一试。谢谢你的提示! :)
    【解决方案4】:

    我已经建立了一个名为 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
    

    我离开了分支网络检查。回到本地机器。

    我通过克隆从服务器中提取,并将我的项目目录内容添加到本地 repo 的分支 master 中。仍然不使用结构,只是进行设置,尽管我想这些步骤也可以自动化,而且它们都不需要另一个 ssh 密码。

       cd /path/to/project_name/..
       git clone ssh://joe@some_server.com/var/web/project_name/.git
       cd project_name
       gvim fabfile.py
       git add  fabfile.py
       git commit -a -m "fabfile edits"
    

    现在我开始使用织物。下面是从我的 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,我只需对我的项目目录进行一些更改, 想一个关于他们的适当信息,然后做:

    $fab committag
    

    我在服务器上标记并更新了更改。或者:

    $fab committag brpush
    

    我创建了一个新分支并更新了服务器。

    【讨论】:

      【解决方案5】:

      跳过主机验证提示的一种方法是:

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

      另外,我使用py-github 来安装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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-05-15
        • 1970-01-01
        • 2016-02-02
        • 2016-10-10
        • 2018-08-17
        • 2017-07-26
        • 1970-01-01
        相关资源
        最近更新 更多