3

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 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I did not work out. I tried. Is it related to type of input stream keyboard. VT100 ot something. While debugging, I get in input stream the property ttype=vt100. Do we require any \n or \r ?
I tried using echo $key ...... It gives [[12~ on the screen. But how to pass in java inside out.write ? Because "[[12~".getBytes() is not working and also tried \[[12~ and "\u001b\u001b12~".getBytes(). More over ^[[12 also not working.
I think, your output must be '^[[12~', not '[[12~'. In this case my Java code would be the ok. If it is not working, I suppose the problem must be elsewhere. Maybe the program running in the remote terminal expects something else. Which command did you start on the remote system in this session?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.