【问题标题】:connecting the setInputStream and setOutputStream of the JSch to jTextField's将 JSch 的 setInputStream 和 setOutputStream 连接到 jTextField 的
【发布时间】:2014-02-25 15:15:46
【问题描述】:

我是 Java 新手,需要帮助。 我使用 jsch 库创建到远程 linux 机器的 SHH 连接。在接下来的代码中,从 JSCH 创建者的教程中给出,您可以看到它实现了 shell 通信,它是来自 cmd 窗口的直接通信。您从 cmd 窗口输入命令,然后在 cmd 窗口中得到结果。

String user = "username";
String host = "hostname";

session session=jsch.getSession(user, host, 22);
String passwd = JOptionPane.showInputDialog("Enter password");
session.setPassword(passwd);
Channel channel=session.openChannel("shell");
session.connect();
channel.setInputStream(System.in);
channel.setOutputStream(System.out);

我如何将接下来的两行连接到 jTextField1 和 jTextField2。举个例子,我想从 jTextField1 发送命令并从 jTextField2 获取结果。

channel.setInputStream(System.in);
channel.setOutputStream(System.out);

在此感谢您

【问题讨论】:

    标签: shell connection jsch


    【解决方案1】:

    您可以提供所需的流,而不是 System.in。例如

    String ls = "ls \n"; // here i am passing a string to stream, you  can pass as you desire
    InputStream in = new ByteArrayInputStream(ls.getBytes("UTF-8"));
    channel2.setInputStream(in);//you passed the string as input 
    
    
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(out);
    channel2.setOutputStream(ps);
    String result = out.toString();//here we get output to a string which you can add to your file.
    

    这是一种选择 可能有更好的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-20
      • 1970-01-01
      • 1970-01-01
      • 2021-09-25
      相关资源
      最近更新 更多