即时更改 System.in 并以编程方式读取 System.out (Jsch)

Change System.in and read System.out programmly on the fly (Jsch)

在我的代码中,我试图通过 SSH 运行 在远程服务器上执行一些命令。 这些命令必须相互建立,但背后有逻辑。这意味着: 当命令 a 的输出包含“retcode 0”时,则执行命令 b。否则执行命令 c

我发现没有办法将这个逻辑实现到几个“exec”命令中。似乎每个“exec”都有自己的进程,所以我无法继续之前的地方。有了一个“exec”,我就可以传递一个命令列表,所有命令都将在其中执行,所以那里没有逻辑。所以,我决定对 Jsch 使用“shell”。 (如果有办法为它使用 exec,我会很高兴)

基于jcraft的例子,我写了这段代码:

import com.jcraft.jsch.*;
import java.io.ByteArrayInputStream;
import java.io.InputStream;

public class Main {
    public static void main(String[] args) {
        try {
            JSch jsch = new JSch();
            String user = "sshuser";
            String host = "localhost";
            Session session = jsch.getSession(user, host, 22);
            String passwd = "password";
            session.setPassword(passwd);
            session.setConfig("StrictHostKeyChecking", "no");

            //session.connect();
            session.connect(30000);   // making a connection with timeout.
            Channel channel = session.openChannel("shell");

            // Enable agent-forwarding.
            ((ChannelShell)channel).setAgentForwarding(true);


            // Set Streams
            channel.setInputStream(System.in);
            channel.setOutputStream(System.out);

            channel.connect(3 * 1000);
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

基本上,这给了我作为一个人做我想做的事情的可能性。我可以在 System.in 中输入命令,return 被打印到 System.out。我可以阅读它,决定我想用它做什么,然后输入下一个命令。该命令将准确地在我之前的位置执行,所以一切都很好。

现在我必须想办法通过 java 来做到这一点。我找到了一种通过固定字符串输入第一个命令的方法:

[...]
InputStream testInput = new ByteArrayInputStream( "dir \n".getBytes("UTF-8") );
// Set Streams
channel.setInputStream(testInput);
[...]

但在那之后我发现没有办法发送下一个(作为第一步,即使没有读取输出)。

所以,我的问题是,有没有办法通过 Java 代码设置 System.in,这将直接通过 Jsch (System.setIn()对我不起作用)或另一种即时更改输入字符串的方法,以便它通过 Jsch 传输?

感谢您的宝贵时间!

感谢 Martin Prikryl 的评论,我找到了解决方案。 我用 Telnet 而不是我的真实应用程序创建了一个小例子。基础是相同的,我认为它更有帮助,因为当它不基于特定软件时,更多人可以尝试和使用它。

import com.jcraft.jsch.*;
import java.io.*;

public class Main {
    public static void main(String[] args) {
        OutputStream out = null;
        Session session = null;
        try {
            JSch jsch = new JSch();
            String user = "sshuser";
            String host = "localhost";
            session = jsch.getSession(user, host, 22);
            String passwd = "password";
            session.setPassword(passwd);
            session.setConfig("StrictHostKeyChecking", "no");

            // vars and objects used later
            String lineSeperator = System.getProperty("line.separator");
            StringBuilder sb = new StringBuilder();
            Main main = new Main();

            //session.connect();
            session.connect(30000);   // making a connection with timeout.
            ChannelExec channel = (ChannelExec) session.openChannel("exec");
            InputStream in = channel.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));

            // start telnet session
            channel.setCommand("telnet 192.168.222.128 -l sshuser");
            out = channel.getOutputStream();
            channel.connect();
            // wait a little bit for telnet to be ready to take the input
            Thread.sleep(500);
            // pass the password
            out.write(("password\n").getBytes());
            out.write(("\n").getBytes());
            Thread.sleep(500);
            // flush reader, very important!
            out.flush();

            // Read from Bufferreader until the current line contains a specific string
            // For my real application it would be "---       END", for this example i
            // used something from the last line my machine returns. Very important that this string
            // appears on every possible output, or you will stuck in a while loop!
            //
            // Tried it with while((reader.readline())!=null) but this ends in a infinity loop too.
            // Since in my application there is an String that always get returned i didn't look it further up
            String responeFromLogin = main.readOutput("security updates.", reader, lineSeperator, sb);

            // Working with the response, in this example a simple fail-->Exception, success --> progress
            if (responeFromLogin.contains("Login incorrect")) {
                throw new Exception("Failed: Login");
            }
            System.out.println("Login Successfull");

            // Log in was successful, so lets do the next command, basiclly the same routine again
            out.write(("dir\n").getBytes());
            Thread.sleep(500);
            out.flush();
            // Again, not bulletproofed in this example
            String responseFromHelp = main.readOutput("examples.desktop", reader, lineSeperator, sb);
            if (!responseFromHelp.contains("test")) {
                throw new Exception("Failed: Help");
            }
            System.out.println("Folder Found");



        } catch (InterruptedException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        } catch (JSchException e1) {
            e1.printStackTrace();
        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.flush();
                }
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            System.out.println("_________________________");
            System.out.println("I am done");
            if (session != null) {
                session.disconnect();
            }
        }
    }

    public String readOutput(String endString, BufferedReader reader, String lineSeperator, StringBuilder sb) {
        String line;
        String returnString = "Error";

        while (true) {
            try {
                line = reader.readLine();
                if (line.contains(endString)) {
                    sb.append(line).append(lineSeperator);
                    returnString = sb.toString();
                    break;
                } else {
                    sb.append(line).append(lineSeperator);
                }
            } catch (IOException e) {
                returnString = "Error";
                e.printStackTrace();
            }
        }
        return returnString;
    }
}