2012-02-07 36 views
2

我有以下fabfile:使用织物在受限shell执行命令

from fabric.api import * 

env.hosts = ['samplehost'] 
env.user = 'foo' 
env.password = 'bar' 
env.shell = '' 

def exec_ls(): 
    run('ls') 
    run('ls -l') 

,我得到下面的输出:

[samplehost] Executing task 'exec_ls' 
[samplehost] run: ls 
[samplehost] out: sample.txt 

[samplehost] run: ls -l 
[samplehost] out: rbash: ls -l: command not found 

Fatal error: run() encountered an error (return code 127) while executing 'ls -l' 

Aborting. 
Disconnecting from samplehost... done. 

的登录shell用户“富”是' /斌/ rbash”。

看来,如果我用参数执行一个命令,它将被视为单个命令(而'ls'没有参数完美工作)。

请注意,我已经放了一个空壳,因为否则Fabric会尝试使用'/ bin/bash',并且这是受限制的shell所不允许的。

是否可以在受限制的外壳中使用Fabric?

回答

2

该问题与使用rbash这一事实无关,而与env.shell的空值有关。为了解决这个问题使用:

env.shell = '/bin/rbash -l -c' 

需要注意的是:

  • env.shell的默认值是/bin/bash -l -c,因此使用/bin/rbash -l -c有道理
  • env.shell设置为空字符串,命令ISN通过任何shell执行
  • shell是一个负责将长字符串拆分为命令和参数的程序,而不使用shell将所有字符串解释为单个com普通话是不会因为它发生
+0

指定''rbash -l -c'',解决了这个问题。由于受限制的shell不允许使用绝对路径,所以不能放入'/ bin/rbash'。 – alexyz78 2012-02-10 15:32:03

1

- 检查目标机的环境

echo $SHELL 

.Hypothetically你得到这个发现:

/bin/sh 

- 然后在你的python fabfile.py中:

from fabric.api import env 

env.shell = "/bin/sh -c" 
2

在我的环境中,你将一个受限制的shell作为Pure数组的一部分唱出来,看起来一个选项是将参数shell = False传递给run函数。