简体   繁体   English

JSch Esc命令和编码

[英]JSch Esc commands and encoding

I'm writing an application to connect to a server over SSH, open an application (it runs on shell) and use it. 我正在编写一个应用程序,以通过SSH连接到服务器,打开一个应用程序(在shell上运行)并使用它。 I'm using JSch and I've managed to connect, open the app and send commands to it. 我正在使用JSch,并且已经设法连接,打开应用程序并向其发送命令。 Now I'm facing 2 problems: 现在我面临两个问题:

  1. On Windows I'm having encoding problems with the app (on Linux if I use the IDE console output to run it I have the same problem, it only shows right on bash). 在Windows上,我的应用程序存在编码问题(在Linux上,如果我使用IDE控制台输出来运行该应用程序,我会遇到同样的问题,仅在bash上正确显示)。 Here's a screehshot: 这是一个快照: 编码错误 here's the screenshot of what it should show: 这是它应显示的屏幕截图: 正确编码
  2. The app I'm running, makes use of the ESC key and I cannot use it in the app I've write. 我正在运行的应用程序使用了ESC键,但无法在编写的应用程序中使用它。 It just writes ^[ on the output And here are the code I've used to do this application: 它只是在输出上写^[ ,这是我用来执行此应用程序的代码:

     JSch shell = new JSch(); Session session = shell.getSession("myUser", "myHost"); session.setPassword("myPassword"); session.setConfig("StrictHostKeyChecking", "no"); session.connect(); Channel channel = session.openChannel("shell"); channel.setOutputStream(System.out); PipedInputStream in = new PipedInputStream(); PipedOutputStream out = new PipedOutputStream(in); channel.setInputStream(in); channel.connect(); out.write("my_application\\n".getBytes()); Thread.sleep(1000); //waiting for the app to load out.write("\\n".getBytes()); out.write("appUser\\n".getBytes()); out.write("appPassword\\n".getBytes()); channel.setInputStream(System.in); 

PS: the server uses sh and not bash. PS:服务器使用sh而不是bash。

As for your first question, those are ANSI escape codes. 关于第一个问题,这些是ANSI转义码。 When you create a Jsch shell session, by default a pseudo-terminal will be allocated - this causes those sequences to be send. 创建Jsch Shell会话时,默认情况下将分配一个伪终端-这将导致发送这些序列。 You can disable the pseudo terminal: 您可以禁用伪终端:

((ChannelShell)channel).setPty(false);

For more, see this discussion . 有关更多信息,请参见此讨论

With regard to your second question, the problem is that on Windows Java's System.in is buffered, and will not allow you to relay the escape key easily. 关于第二个问题,问题是在Windows Java的System.in已缓冲,并且不允许您轻松中继转义键。 You should be able to read lines of text and send them to the remote application. 您应该能够阅读文本行并将其发送到远程应用程序。 For anything more, you'll have to resort to native code to get the actual key presses. 除此之外,您还必须诉诸本机代码才能获得实际的按键效果。 See this discussion for more details on how to do that on different operating systems. 有关如何在不同操作系统上执行此操作的更多详细信息,请参见此讨论

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

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