I use the jsch to execute interactive commands(that prompt for some input/password) on the remote hosts which work perfectly fine.
There are some interactive commands which require a terminal for execution eg. sudo/su.
For these commands I set the pty flag to true ie "((ChannelExec)channel).setPty(true)"
This is also working perfectly fine, but for some instances the execution hangs, the reason being the input/password is being piped even before the pseudo terminal is launched
and hence the command is waiting infinitely for the password/input.
The following is the code snippet used .
//Set the pseudo terminal allocation flag to true
// Set the input stream with the input to be passed for the remote command
byte cmdbuf = cmdInput.getBytes();
ByteArrayInputStream bi = new ByteArrayInputStream(cmdbuf);
//Set the channel's error stream
ByteArrayOutputStream bo = new ByteArrayOutputStream();
//Get the input stream of the channel
byte tmp=new byte;
int i=in.read(tmp, 0, 1024);
System.out.print(new String(tmp, 0, i));
//Hang is detected since the password is pumped in even before the pseudo terminal is launched and now the command(sudo/su) is waiting infinitely for the password.
How do I go about resolving this issue.
i. Is there a way to introduce a delay or ensure that the pseudo terminal and the command is launched and only then the input/password is piped in.
ii. If the above option is not feasible, is there a way to detect this hang and once again set the input/password in the InputStream.
+-From: prakash babu <jprakashbabu@...> --
|_Date: Mon, 28 Nov 2011 12:19:17 +0530 (IST) ____
|This is also working perfectly fine, but for some instances,A (B
|the execution hangs, the reason being the input/password is being
|piped even before the pseudo terminal is launched and hence
|the command is waiting infinitely for the password/input.
You should wait for the prompt for password, and then send the password,
as we needed to do in dial-up connections at the ancient days
with ppp, slip, uucp, etc.
|How do I go about resolving this issue.
If it is acceptable to use flags '-S' and '-p' options of sudo command,
1-14-20 HONCHO AOBA-KU,
SENDAI, MIYAGI 980-0014 Japan.