简体   繁体   English

Python Fabric:如何处理任意远程shell提示输入?

[英]Python Fabric: How to handle arbitrary remote shell prompt for input?

This is related to this question here, but with a slight twist: instead of just passing 'yes' or 'no', I need Fabric to pass an arbitrary string to the remote shell. 这与这个问题有关,但略有不同:我不需要传递'yes'或'no',而是需要Fabric将任意字符串传递给远程shell。

For instance, if the remote shell prompts for 'what is your name?' 例如,如果远程shell提示“你叫什么名字?” then I need to feed it 'first,last'. 然后我需要'先,最后'喂它。

Clarification: I know I said arbitrary input, but I was really trying to use it for the SSH key passwd prompt when I try to do a git pull . 澄清:我知道我说过任意输入, 但当我尝试做一个git pull时,我真的试图将它用于SSH密钥passwd提示

Update #1: Got a response from Jeff Forcier @bitprophet 更新#1:得到了Jeff Forcier @bitprophet的回复

I have proposed an API for this feature in fabric on the mailinglist, and ended up writing something myself: 我已经在邮件列表的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')

See my blogpost on expecting prompts in fabric with fexpect 请参阅我的博客文章, 期待面料中的提示与fexpect

Interaction with remote servers is finally supported in Fabric 1.0. Fabric 1.0最终支持与远程服务器的交互。 See this page for details. 有关详情,请参阅此页面

也许看看pexpect

I've set up a git origin repository called project_name/.git. 我已经建立了一个名为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

I leave branch web checked out. 我离开了分支网络。 Back to the local machine. 回到本地机器。

I pull from the server via clone and added my project dir contents in branch master on the local repo. 我通过克隆从服务器拉出来,并在本地仓库的分支主机中添加了我的项目目录内容。 Stll not using fabric, just setting things up, although these steps could be automated too, I suppose, and none of them need another ssh passphrase. 虽然这些步骤也可以实现自动化,但我认为它们不会使用结构,只是设置,而且它们都不需要另一个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"

Now I start using fabric. 现在我开始使用面料。 Below is excerpted from my fabfile for managing git tags and branches: 下面摘自我的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)

To use the above fabfile defs, I just make some changes to my project dir, think of an apporpriate message about them, and do: 要使用上面的fabfile defs,我只是对我的项目目录进行一些更改,想一下关于它们的适当消息,然后执行:

$fab committag

and I have changes tagged and updated on the server. 我在服务器上标记和更新了更改。 Or: 要么:

$fab committag brpush

and I have a new branch created and the server updated. 我创建了一个新分支并更新了服务器。

One way of skipping the host verification prompt is: 跳过主机验证提示的一种方法是:

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

Also, I'm using py-github to install the deploy keys: 另外,我正在使用py-github来安装部署密钥:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM