1

我正在使用 Primefaces 的终端组件和 JSch 来 ssh 到远程桌面。使用 exec 通道执行需要花费太多时间,因为会话和通道都会在每个命令处关闭,而我没有设法避免这一点。所以我将频道更改为 shell,现在我正在尝试“重定向”标准输入/输出蒸汽。这是我的代码的样子:

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

@ManagedBean
@SessionScoped
public class TerminalController implements Serializable{  

     public TerminalController(){
     jsch=new JSch();
     InputStream in=null;
     PrintStream out=System.out;
try{
                      session=jsch.getSession(user, ip, port);
                      session.setConfig("StrictHostKeyChecking", "no");
                        session.setPassword(passwd);
                        session.connect();
                       channel=session.openChannel("shell");    
                       channel.setInputStream(in);
                       channel.setOutputStream(out);
                       channel.connect();
}catch(Exception ee){
                      System.out.println(ee);
} }
    public String handleCommand(String command, String[] params) {  
     command=command+StringUtils.join(params," ");

                   in=IOUtils.toInputStream(command);
                  String result=out.toString();
                   out.flush();
                    return result;}

我知道这是一团糟,我仍然是java的初学者。我想到的另一个问题是,在从iostream到字符串的转换中,我可能会丢失回车按钮功能!我正在等待您的建议、解决方案和建议。

4

2 回答 2

1
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.apache.commons.lang.StringUtils;
import org.fusesource.jansi.AnsiConsole;
import org.fusesource.jansi.AnsiString;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

@ManagedBean
@SessionScoped
public class TerminalController {
    BufferedReader fromChannel;
    PrintWriter toChannel;
    Channel channel;
    Session session;
    JSch jsch;

    public TerminalController() {
        jsch = new JSch();
        try {
            session = jsch.getSession("leoks", "localhost", 22);
            session.setConfig("StrictHostKeyChecking", "no");
            session.setPassword("xxx");
            session.connect(Integer.MAX_VALUE); // 3 secs timeout
            channel = session.openChannel("shell");
            InputStream inStream = channel.getInputStream();
            fromChannel = new BufferedReader(new InputStreamReader(inStream,
                    "UTF-8"));
            OutputStream outStream = channel.getOutputStream();
            toChannel = new PrintWriter(new OutputStreamWriter(outStream,
                    "UTF-8"));
            channel.connect();

            StringBuilder result = new StringBuilder();
            boolean stop = false;
            while (!stop) {
                // prompt does not end with newline...
                char c = (char) fromChannel.read();
                result.append(c);
                System.out.print(c);
                if (result.toString().endsWith("$")) {
                    System.out.print("<<<");
                    stop = true;
                }
            }

        } catch (Exception ee) {
            ee.printStackTrace();
        }
    }

    public String handleCommand(String command, String[] params)
            throws IOException {
        command = command + " " + StringUtils.join(params, " ");
        toChannel.println(command);
        toChannel.flush();
        StringBuilder result = new StringBuilder();
        AnsiConsole.systemInstall();
        while (true) {
            char c = (char) fromChannel.read();
            result.append(c);
            if (c == '$') {
                AnsiString as = new AnsiString(result.toString());
                String s = as.getPlain().toString();
                s = s.replaceAll("\n", "<br>");
                AnsiConsole.systemUninstall();
                return s;
            }
        }
    }
}

Jansi 还有一个 HTML 渲染类,它需要一些工作才能将 \n 正确替换为 BR,因为终端组件会渲染 HTML 并默默地忽略任何包含控制代码的格式错误的字符串。您可能还想用固定宽度的字体替换终端 CSS 定义,例如,使用诸如

.ui-widget
{
    font-family: "Courier New", Courier, monospace;
    font-size: 0.8em;
}

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui">
<h:head>
    <title>Terminal POC</title>
</h:head>
<h:body>
    <h:outputStylesheet library="css" name="styles.css" />
    <h:form>
        <p:terminal 
            commandHandler="#{terminalController.handleCommand}"
            height="300px" 
            welcomeMessage="Welcome..." 
            prompt="&gt;" />
    </h:form>
</h:body>
</html>
于 2013-05-28T17:09:45.423 回答
0

设置in变量对通道没有影响,调用out.toString()不会给你任何结果。

您需要将命令(及其参数)写入通道,然后从那里读取输出。另外,不要使用setInputStreamorsetOutputStream方法(如果您有现有的流可以读取或写入,它们很好,但您没有)。

public class TerminalController implements Serializable{  

    BufferedReader fromChannel;
    PrintWriter toChannel;
    Channel channel;
    Session session;

     public TerminalController(){
        jsch=new JSch();
        try {
            session=jsch.getSession(user, ip, port);
            session.setConfig("StrictHostKeyChecking", "no");
            session.setPassword(passwd);
            session.connect();
            channel = session.openChannel("shell");    
            InputStream inStream = channel.getInputStream();
            fromChannel = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
            OutputStream outStream = channel.getOutputStream();
            toChannel = new PrintWriter(new OutputStreamWriter(outStream, "UTF-8"));
            channel.connect();
        } catch(Exception ee) {
            ee.printStackTrace();
        }
    }

    public String handleCommand(String command, String[] params) {  
         command = command + StringUtils.join(params," ");
         toChannel.println(command);
         StringBuilder result = new StringBuilder();
         while(true) {
            String line = fromChannel.readLine();
            result.append(line);
            if(looksLikePrompt(line))
               return result.toString();
            result.append("\n");
         }
    }
}

困难的部分实际上是知道远程命令的输出何时完成,并且您需要返回(这是looksLikePrompt我不知道如何编写的方法)。

我不确定CommandHandlerPrimeface 终端组件的原理是创建交互式终端的正确设计,终端不需要知道命令何时完成。当命令需要更多输入时会发生什么?

于 2012-09-22T18:09:30.883 回答