简体   繁体   English

使用Fabric在受限Shell中执行命令

[英]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: 注意:

  • the default value for 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很有意义
  • when env.shell is set to the empty string, the command isn't executed through any shell env.shell设置为空字符串时,该命令不会通过任何shell执行
  • the shell is the one that takes care of splitting long strings into commands and arguments, without the shell all the string is interpreted as a single command that isn't going to be found as it was happening 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM