繁体   English   中英

如何运行独立/交互式Spring Boot CRaSH Shell应用程序?

[英]How to run a standalone/interactive Spring Boot CRaSH Shell application?

我想运行一个嵌入CRaSH Shell的Spring Boot应用程序,但我希望CRaSH Shell能够在Spring初始化完成后立即在当前控制台(即直接/独立)中启动而无需任何密码,而不是通过SSH / Telnet访问所有的豆子。

当用户键入exit或按Ctrl + D时,应用程序应关闭。

另外,应该禁用SSH和Telnet支持。

PS。 如果应用程序可以从stdin读取命令,则可以加分

./crshapp < somefile.cmd

cat somefile.cmd | ./crshapp

我遇到了同样的问题,因此我实现了以下代码以将其附加到由boot配置的正在运行的外壳程序上。

我是通过从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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM