[英]Use Fabric to execute commands in a restricted shell
I've the following fabfile: 我有以下fabfile:
from fabric.api import *
env.hosts = ['samplehost']
env.user = 'foo'
env.password = 'bar'
env.shell = ''
def exec_ls():
run('ls')
run('ls -l')
and I get the following output: 我得到以下输出:
[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.
The login shell for user 'foo' is '/bin/rbash'. 用户“ foo”的登录外壳为“ / bin / rbash”。
It seems that if I execute a command with parameters it is treated as a single command (while 'ls' without parameters works perfectly). 看来,如果我执行带有参数的命令,它将被视为单个命令(而没有参数的“ ls”则可以完美运行)。
Please note that I've put an empty shell because otherwise Fabric tries to use '/bin/bash' and that's not allowed by he restricted shell. 请注意,我放置了一个空的shell,因为否则Fabric会尝试使用“ / bin / bash”,并且受限的shell不允许这样做。
Is it possible to use Fabric in a restricted shell? 是否可以在受限制的外壳中使用Fabric?
在我的环境中,使用受限制的shell作为Pure数组的一部分,似乎可以选择将参数shell = False传递给run函数。
The problem isn't related to the fact that rbash
is being used, but to the the empty value of env.shell
. 问题与正在使用
rbash
的事实rbash
,而与env.shell
的空值有关。 To fix that problem use: 要解决该问题,请使用:
env.shell = '/bin/rbash -l -c'
Note that: 注意:
env.shell
is /bin/bash -l -c
, so using /bin/rbash -l -c
makes sense env.shell
的默认值为/bin/bash -l -c
,因此使用/bin/rbash -l -c
很有意义 env.shell
is set to the empty string, the command isn't executed through any shell env.shell
设置为空字符串时,该命令不会通过任何shell执行 -Check the environment of the target-machine with -用以下命令检查目标机器的环境
echo $SHELL
.Hypothetically you get this: 假设你得到这个:
/bin/sh
-Then in your python fabfile.py: -然后在您的python fabfile.py中:
from fabric.api import env
env.shell = "/bin/sh -c"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.