我正在尝试通过远程 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: line 0: syntax error near unexpected token
'weblogic','password','t3://host:7001''<br/> 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();
结果:
Initializing WebLogic Scripting Tool (WLST) ...
Welcome to WebLogic Server Administration Scripting Shell
Type help() for help on available commands
wls:/offline>
我手动测试了该命令并且它有效。问题似乎出在带有 WLST 接口(interface)的 JSch 上,因为它打开了另一个 shell 接口(interface)。
有什么想法可以使用 JSch 运行 WLST 命令吗?
PS1:我知道我的 JSch 代码可以工作,因为我在同一个应用程序上有一个功能需要部署。基本上,它运行 jscp 来上传 war,然后运行 ssh 来执行 weblogic.Deployer -deploy 命令。
PS2:我确实有一个 .py 脚本可以做到这一点,但到目前为止,它必须在服务器上才能执行。我正在考虑对临时文件夹执行 jscp,运行脚本,然后删除。但我很好奇如何使用 JSch 在 WLST 上运行多个命令。
提前致谢。
更新
代码工作(感谢马丁)
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();
最佳答案
;
确实可以在基于 *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基本相同.
关于java - 使用 JSch 调用 WLST 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46549354/