我有以下 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/