我想运行嵌入CRaSH shell的Spring Boot应用程序,但不是通过SSH / Telnet访问,我希望CRaSH shell在当前控制台(即直接/独立)中启动而无需任何密码,如Spring完成所有bean的初始化。
当用户键入exit
或按Ctrl + D时,应用应该关闭。
此外,应禁用SSH和Telnet支持。
PS。如果应用程序可以从stdin读取命令,例如
,则奖励积分./crshapp < somefile.cmd
cat somefile.cmd | ./crshapp
答案 0 :(得分:3)
我有同样的问题,所以我实现了以下代码以附加到由boot配置的正在运行的shell。
我通过复制org.crsh.standalone.CRaSH中的一些代码来完成此操作,该代码加载了一个独立的shell。
import org.crsh.console.jline.JLineProcessor;
import org.crsh.console.jline.Terminal;
import org.crsh.console.jline.TerminalFactory;
import org.crsh.console.jline.console.ConsoleReader;
import org.crsh.plugin.PluginLifeCycle;
import org.crsh.shell.Shell;
import org.crsh.shell.ShellFactory;
import org.crsh.util.InterruptHandler;
import org.fusesource.jansi.AnsiConsole;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.CommandLineRunner;
import java.io.*;
public class InteractiveShellRunner implements CommandLineRunner, InitializingBean, DisposableBean {
final private PluginLifeCycle crshBootstrapBean;
private Shell shell;
private Terminal term;
public InteractiveShellRunner(PluginLifeCycle crshBootstrapBean) {
this.crshBootstrapBean = crshBootstrapBean;
}
@Override
public void afterPropertiesSet() throws Exception {
ShellFactory shellFactory = crshBootstrapBean.getContext().getPlugin(ShellFactory.class);
shell = shellFactory.create(null);
}
@Override
public void destroy() throws Exception {
try {
if (term != null) {
term.restore();
}
} catch (Exception ignore) {
}
}
@Override
public void run(String... args) throws Exception {
if (shell != null) {
term = TerminalFactory.create();
//
String encoding = jline.internal.Configuration.getEncoding();
// Use AnsiConsole only if term doesnt support Ansi
PrintStream out;
PrintStream err;
boolean ansi;
if (term.isAnsiSupported()) {
out = new PrintStream(new BufferedOutputStream(term.wrapOutIfNeeded(new FileOutputStream(FileDescriptor.out)), 16384), false, encoding);
err = new PrintStream(new BufferedOutputStream(term.wrapOutIfNeeded(new FileOutputStream(FileDescriptor.err)), 16384), false, encoding);
ansi = true;
} else {
out = AnsiConsole.out;
err = AnsiConsole.err;
ansi = false;
}
//
FileInputStream in = new FileInputStream(FileDescriptor.in);
ConsoleReader reader = new ConsoleReader(null, in, out, term);
//
final JLineProcessor processor = new JLineProcessor(ansi, shell, reader, out);
//
InterruptHandler interruptHandler = new InterruptHandler(processor::interrupt);
interruptHandler.install();
//
Thread thread = new Thread(processor);
thread.setDaemon(true);
thread.start();
try {
processor.closed();
} catch (Throwable t) {
t.printStackTrace();
}
}
}
}
剩下的就是将其加载到上下文中,如下所示:
@Configuration
@AutoConfigureAfter(CrshAutoConfiguration.class)
public static class ShellConfiguration {
@Bean
InteractiveShellRunner runner(@Qualifier("shellBootstrap") PluginLifeCycle crshBootstrapBean){
return new InteractiveShellRunner(crshBootstrapBean);
}
}