无法从su运行cmd-<user>

时间:2018-10-31 10:41:36

标签: java bash jboss

我尝试使用以下cmd从jboss用户(当前用户-root)连接到JBoss:

protected static final String CONNECT = "--connect";
protected static final String EXECUTOR = "/bin/jboss-cli.sh";
protected static final String CONTROLLER = "--controller";
protected static final String SU_CMD = "su";
protected static final String SHELL_FLAG = "-c";


protected static final String COLON = ":";
protected final static String SPACE = " ";
protected final static String QUOTE = "\"";
protected final static String DASH = "-";

public String[] getCommandWithController(String serverPath, String server_path, String server_port) {
        String[] cmd = {SU_CMD, DASH, JBoss.getUsername(), SHELL_FLAG, serverPath + EXECUTOR + SPACE + CONNECT + SPACE + CONTROLLER + "=" + server_path + COLON + server_port, this.getCommand()};
        return cmd;
    }

对于Runtime.getRuntime().exec(anCmdArr),它看起来像:

su-jboss -c /jboss-as-7.1.1.Final/bin/jboss-cli.sh --connect --controller = localhost:10999:read-resource(recursive = true,递归- depth = 10,include-runtime = true)

但是我的控制台挂起了。同时,如果我手动运行它,它将起作用:

# su - jboss -c "/jboss-as-7.1.1.Final/bin/jboss-cli.sh --connect --controller=localhost:10999"

[standalone@localhost:10999 /] :read-resource(recursive=true, recursive-depth=10, include-runtime=true)

我认为cmd中的问题。我已经厌倦了引号,像这样的字符串连接:

1)

String[] cmd = {SU_CMD + SPACE + DASH + SPACE + JBoss.getUsername() + SPACE + SHELL_FLAG + QUOTE + serverPath + EXECUTOR + SPACE + CONNECT + SPACE + CONTROLLER + "=" + server_path + COLON + server_port + QUOTE, this.getCommand()}

它看起来像:

su-jboss -c“ /jboss-as-7.1.1.Final/bin/jboss-cli.sh --connect --controller = localhost:10999”

2)

String[] cmd = {SU_CMD, DASH, JBossTaskConstants.getUsername(), SHELL_FLAG, QUOTE + serverPath + EXECUTOR + SPACE + CONNECT + SPACE + CONTROLLER + "=" + server_path + COLON + server_port + QUOTE, this.getCommand()};

外观:

su-jboss -c“ /jboss-as-7.1.1.Final/bin/jboss-cli.sh --connect --controller = localhost:10999”:读取资源(递归= true, recursive-depth = 10,include-runtime = true)

但是我只能得到的是:

error = 2, No such file or directory

例如: 错误消息::read-resource(recursive = true,recursive-depth = 10,include-runtime = true):/ jboss-as-7.1.1.Final/bin/jboss-cli.sh --connect --controller =本地主机:10999:没有这样的文件或目录

我可以更改什么?

1 个答案:

答案 0 :(得分:1)

当我们使用 -c 参数时,bash只需要一个命令。因此,我的旧cmd是错误的,因为它包含两个单独的cmd:

  1. /jboss-as-7.1.1.Final/bin/jboss-cli.sh --connect --controller = localhost:10999 命令已发送到 jboss 会议。
  2. 但是:read-resource(recursive = true,recursive-depth = 10,include-runtime = true)只是 root 会话的参数。

所以我应该像参数一样将1和2发送到-c选项。我删除了一个逗号, 逃脱了2cmd的引号:

String[] cmd = {SU_CMD, DASH, JBossTaskConstants.getUsername(), SHELL_FLAG, serverPath + EXECUTOR + SPACE + CONNECT + SPACE + CONTROLLER + "=" + server_path + COLON + server_port + SPACE + "\'" + this.getCommand() + "\'"};

什么是:

su-jboss -c /jboss-as-7.1.1.Final/bin/jboss-cli.sh --connect --controller = localhost:10999':read-resource(recursive = true,递归-depth = 10,include-runtime = true)'

要手动运行它,应使用双引号,因为单引号具有更高的优先级:

su-jboss -c“ /jboss-as-7.1.1.Final/bin/jboss-cli.sh --connect --controller = localhost:10999':read-resource(recursive = true, recursive-depth = 10,include-runtime = true)'“