首页 文章

使用JSch调用WLST命令

提问于
浏览
1

我正在尝试通过远程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:第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:/ offline>

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

有关如何使用JSch运行WLST命令的任何想法?

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

PS2:我有一个.py脚本来做到这一点,但截至目前,它必须在服务器上执行 . 我正在考虑对临时文件夹执行jscp,运行脚本然后删除 . 但我很想知道如何使用JSch在WLST上运行多个命令 .

提前致谢 .

UPDATE

代码工作(感谢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();

1 回答

  • 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基本相同 .

相关问题