【问题标题】:Invoke WLST commands with JSch使用 JSch 调用 WLST 命令
【发布时间】:2018-03-14 22:21:53
【问题描述】:

我正在尝试通过远程 Java Web 应用在 WLST 上运行重启服务器命令。

这就是我要执行的操作:

StringBuilder sb = new StringBuilder();
sb.append("/u01/app/oracle/jdk1.8.0_65/bin/./java -cp /u01/app/oracle/product/Oracle_Home/wlserver/server/lib/weblogic.jar weblogic.WLST");
sb.append(";connect(\'weblogic\',\'" + consolePass + "\',\'" + fullAddress + "\')");
sb.append(";domainRuntime()");
sb.append(";cd(\'/ServerLifeCycleRuntimes/" + serverName + "\')");
sb.append(";cmo.shutdown())");
sb.append(";start(" + serverName + ",'Server')");
String command = sb.toString();

JSch jsch = new JSch();
Session session = jsch.getSession(user, host, port);
session.setUserInfo(new OracleUserInfo(pass));
session.connect();

Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
InputStream in = channel.getInputStream();
channel.connect();

byte[] tmp = new byte[1024];
while (true) {
    while (in.available() > 0) {
        int i = in.read(tmp, 0, 1024);
        if (i < 0)
            break;
        System.out.print(new String(tmp, 0, i));
    }
    if (channel.isClosed()) {
        if (in.available() > 0)
            continue;
        System.out.println("exit-status: " + channel.getExitStatus());
        break;
    }
    try {
        Thread.sleep(1000);
    } catch (Exception ee) {
    }
}
channel.disconnect();
session.disconnect();

我正在使用';'分开命令,因为我认为需要运行多个命令。

不幸的是,它在第​​ 2 行给出了语法错误。

bash:-c:第 0 行:意外标记附近的语法错误 'weblogic','password','t3://host:7001''
bash: -c: line 0:
/u01/app/oracle/jdk1.8.0_65/bin/./java -cp /u01/app/oracle/product/Oracle_Home/wlserver /server/lib/weblogic.jar weblogic.WLST;connect('weblogic','password','t3://host:7001')'

我尝试在第一行后面加上\n,结果第一行执行了(所以进入了WLST),但是剩下的命令都没有了。

StringBuilder sb = new StringBuilder();
sb.append("/u01/app/oracle/jdk1.8.0_65/bin/./java -cp /u01/app/oracle/product/Oracle_Home/wlserver/server/lib/weblogic.jar weblogic.WLST\n");
sb.append(";connect(\'weblogic\',\'" + consolePass + "\',\'" + fullAddress + "\')\n");
sb.append(";domainRuntime()\n");
sb.append(";cd(\'/ServerLifeCycleRuntimes/" + serverName + "\')\n");
sb.append(";cmo.shutdown())\n");
String command = sb.toString();

结果:

正在初始化 WebLogic 脚本工具 (WLST) ...
欢迎使用 WebLogic Server 管理脚本 Shell
键入 help() 以获取有关可用命令的帮助
wls:/离线>

我手动测试了该命令并且它有效。问题似乎出在带有 WLST 接口的 JSch 上,因为它打开了另一个 shell 接口。

知道如何使用 JSch 运行 WLST 命令吗?

PS1:我知道我的 JSch 代码有效,因为我在同一个应用程序上部署了一项功能。基本上,它运行一个 jscp 来上传战争,然后 ssh 来执行 weblogic.Deployer -deploy 命令。

PS2:我确实有一个 .py 脚本来执行此操作,但到目前为止,它必须在服务器上才能执行。我正在考虑对临时文件夹执行 jscp,运行脚本然后删除。但我很想知道如何使用 JSch 在 WLST 上运行多个命令。

提前致谢。

更新

代码工作(感谢 Martin)

    Channel channel = session.openChannel("exec");
    ((ChannelExec) channel).setCommand(command);

    InputStream in = channel.getInputStream();
    OutputStream out = channel.getOutputStream();
    ((ChannelExec) channel).setErrStream(System.err);
    channel.connect();
    for (String wlstCommand : wlstCommands) {
        out.write((wlstCommand).getBytes());
    }
    out.flush();

【问题讨论】:

    标签: java ssh weblogic jsch wlst


    【解决方案1】:

    ; 确实可以在基于 *nix 的系统中使用,在一个 shell 命令行中执行多个命令。

    但是你正在执行的不是 shell 命令。这些是 WLST 命令,对吧?所以你必须把它们喂给 WLST。

    像这样:

    Channel channel = session.openChannel("exec");
    ((ChannelExec) channel).setCommand("java -cp /.../weblogic.jar weblogic.WLST");
    OutputStream out = channel.getOutputStream();
    channel.connect();
    out.write(("connect('weblogic'...)\n").getBytes());
    out.write(("domainRuntime()\n").getBytes());
    ...
    

    和泛型Providing input/subcommands to command executed over SSH with JSch基本一样。

    【讨论】:

      猜你喜欢
      • 2013-06-25
      • 2015-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多