Question

I am connecting to "/bin/bash" in ubuntu using:

process = Runtime.getRuntime().exec(cmd);

Here cmd is a String with different commands that i read and write from process.
Now i have come across a situation, where i login to remote machiens using ssh and while reading writing information to ext process, for loggin out from remote machine, i have to send control character like:

CTRL + ] in order to logout the session gracefully and come back to my local machine. Assuming the cmd is a String type, how can i write this CTRL chracter to the process?

Was it helpful?

Solution

Control-] in ASCII is equivalent to 035 octal. In Java you can represent this as "\035".

Writer writer = new OutputStreamWriter(process.getOutputStream());
writer.write("\035");
writer.flush();

It is also equivalent to decimal value of 29 so if you can write a byte with a value of 29 then that will work as well.

OutputStream os = process.getOutputStream();
os.write(29);
os.flush();

I assume Control-] has to do with the remote program. You are talking about telnet? However writing "exit\n" will also close the remote bash.

Writer writer = new OutputStreamWriter(process.getOutputStream());
writer.write("exit\n");
writer.flush();

You can also, obviously, close the OutputStream which closes the STDIN of the remote process.

os.close();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top