在远程执行python fabric命令之前始终给出命令行

时间:2013-09-12 23:37:13

标签: python ubuntu deployment fabric rackspace

我刚开始使用python fabric在远程rackspace ubuntu服务器上运行命令。如果不让命令行需要输入,我就无法运行命令。这是我重新创建问题的顺序。

使用密钥和无密码连接到远程计算机时,我没有任何问题。

这是我的fabfile.py

from fabric.api import env, run

env.hosts = ['remote.ip.address']
env.user = 'http'

def hello():
    run("touch hello.world")

我用来运行fabfile.py的命令(使用python 2.7 virtualenv)

$ ~/env/py27/bin/fab -f code/fabfile.py hello

这是显示它总是卡住的命令行,并要求我键入exit。

[remote.ip.address] Executing task 'hello'
[remote.ip.address] run: touch hello.world
[remote.ip.address] out: http@server-name:~$ 

我可以通过键入exit退出远程终端,命令将运行,我将返回本地终端。

[remote.ip.address] out: exit
[remote.ip.address] out: 

Done.
Disconnecting from remote.ip.address... done.
me@local-computer: $ 

这是我用来设置ssh密钥以不需要密码的方法。 https://kb.mediatemple.net/questions/1626/Using+SSH+keys+on+your+server

2 个答案:

答案 0 :(得分:1)

有趣的是,我没有这样的问题,您的代码适用于我(最多可添加env.key_filenameenv.password

c:\work>fab hello
[x.x.x.x] Executing task 'hello'
[x.x.x.x] run: touch hello.world

Done.
Disconnecting from x.x.x.x... done.

我正在使用Fabric 1.7.0和Paramiko 1.11.0。 可能这是您服务器上终端的问题。

答案 1 :(得分:1)

好的我明白了。

我编辑了我的〜/ .profile文件以包含“bash”命令,以便bash成为默认shell。我已经复制了下面的〜/ .profile文件的旧版本。由于fabric使用/ bin / bash引导它的所有命令,它实际上是启动两个bash提示并且只退出一个。从服务器上删除bash命令后,一切正常。

这是带有额外bash命令的bash文件。

bash  #REMOVE THIS LINE AND FABRIC WORKS

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
    . "$HOME/.bashrc"
    fi
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi