python - 使用 Fabric 在受限 shell 中执行命令

标签 python fabric

我有以下 fabfile:

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.

用户“foo”的登录 shell 是“/bin/rbash”。

看起来,如果我执行带参数的命令,它会被视为单个命令(而不带参数的“ls”效果很好)。

请注意,我放置了一个空 shell,因为否则 Fabric 会尝试使用“/bin/bash”,而受限 shell 不允许这样做。

是否可以在受限 shell 中使用 Fabric?

最佳答案

该问题与使用 rbash 无关,而是与 env.shell 的空值有关。要解决该问题,请使用:

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

请注意:

  • env.shell 的默认值为 /bin/bash -l -c,因此使用 /bin/rbash -l -c > 有道理
  • env.shell 设置为空字符串时,该命令不会通过任何 shell 执行
  • shell 负责将长字符串拆分为命令和参数,如果没有 shell,所有字符串都会被解释为单个命令,而不会在发生时被发现

关于python - 使用 Fabric 在受限 shell 中执行命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9181153/

相关文章:

python - PyQt4 QPixmap 根据 EXIF 旋转 Jpg

python - 如何捕获和管理GPS模块的异常?

ubuntu - 在 Fabric 脚本中调用 bash 别名

python - 如何在文本文件(而不是代码)中指定主机列表或角色定义?

fabric - 是否可以在本地文件上使用 fabric.contrib.files.append() ?

python - 如何为可能没有值的许多值中的任何一个打印 'null'

python - 德语词干分析器不会删除女性后缀 "-in"和 "-innen"

python - MySQL - 如果表名称包含表列表的记录,则选择表名称

python-2.7 - 使用 Homebrew 和 PIP 让 Fabric 在 Mountain Lion 上工作

linux - 通过fabric脚本在远程服务器上设置umask