英文:
How to send keyboard function keys to SSH server from java code
问题
以下是翻译好的部分:
有一个菜单项在会话连接后显示在控制台上。这些菜单项具有使用 F1、F2、F3 键的命令。
String host = "myhost.abc.123";
String user = "devuser";
String password = "dev1234";
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
Session session = null;
ChannelShell channel = null;
session = jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig(config);
session.connect();
System.out.println("已连接");
channel = (ChannelShell) session.openChannel("shell");
OutputStream out_s = channel.getOutputStream();
channel.connect();
----------
我想发送硬编码的转义序列或从键盘发送 F2 功能键到服务器。
尝试了以下选项,但没有得到下一个响应/输出。
/* --- 用于在控制台/屏幕上打印菜单项的代码 ---
菜单项被打印到控制台
F1 - 添加 F2 - 更新 F3 - 删除
*/
// 尝试 1. F2 - ESC[OQ
out_s.write("\033[OQ".getBytes()); // 可能需要 \n 或 \r 来输入命令
out_s.flush();
// 尝试 2. F2 - ESC[12~
out_s.write("\033[12~".getBytes()); // 可能需要 \n 或 \r 来输入命令
out_s.flush();
// 尝试 3. F2 - \E[12~
out_s.write("\\E[12~".getBytes());
out_s.flush();
// 尝试 4. F2 - 0x71
out_s.write((byte) 0x71); // 可能需要 \n 或 \r 来输入命令
out_s.flush();
// 尝试 5. F2 - \ESC[OQ
out_s.write("\\ESC[OQ".getBytes());
out_s.flush();
英文:
There is a menu item that gets displayed on the console after the connection of the session. And that menu items have command with F1, F2, F3
String host="myhost.abc.123";
String user="devuser";
String password="dev1234";
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
Session session = null;
ChannelShell channel = null;
session = jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig(config);
session.connect();
System.out.println("Connected");
channel = (ChannelShell) session.openChannel("shell");
OutputStream out_s = channel.getOutputStream();
channel.connect();
I want to send the F2 - function key as a hardcoded escape sequence or
from the keyboard, to server.
Tried below options but I am not getting the next response/output
/* --- code for printing the menu item on the console/screen ---
MENU ITEMS GET PRINTED ON THE CONSOLE
F1 - ADD F2 - UPDATE F3 - DELETE
*/
//Try 1. F2 - ESC[OQ
out_s.write("\033[OQ".getBytes()); // may require \n or \r to enter the cmd
out_s.flush();
//Try 2. F2 - ESC[12~
out_s.write("\033[12~".getBytes()); // may require \n or \r to enter the cmd
out_s.flush();
//Try 3. F2 - \E[12~
out_s.write("\\E[12~".getBytes());
out_s.flush();
//Try 4. F2 - 0x71
out_s.write((byte)0x71); // may require \n or \r to enter the cmd
out_s.flush();
//Try 5. F2 - \ESC[OQ
out_s.write("\\ESC[OQ".getBytes());
out_s.flush();
答案1
得分: 1
这对我有效:
out.write("\u001b[12~".getBytes());
out.flush();
在我测试的服务器上:
read -n 5 key
这会从标准输入读取 5 个字节,而无需等待回车键。当然,这只是用于测试。您可以在服务器端进行自己的处理。
如何查找特定键发送的内容(在 bash 中):
read key
然后键入(例如)F2 键。终端应该显示类似于这样的内容:
^[[12~
如果没有,尝试以下命令以显示发送到终端的字节:
echo $key | cat -t
在输出中 ^[
代表(不可打印的)转义字符。其他字符代表它们自己。
英文:
This worked for me:
out.write("\u001b[12~".getBytes());
out.flush();
On the server I tested by:
read -n 5 key
This reads 5 bytes from standard input without waiting for a return key. Of course, this is only for test. You can do your own processing on the server side.
How to find out what is being sent by a specific key (in bash):
read key
Then type (for example) the F2 key. The terminal should show something like this:
^[[12~
If not, try this command to show the bytes sent to the terminal:
echo $key | cat -t
In the output ^[
stands for the (non printable) escape character. The other characters stand for themselves.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论