How to execute docker exec -it netvertex bash
by Java Program.
By this i am trying to execute but not working
public void execute(String command) {
try {
System.out.println("===========>" + command);
Channel channel1 = session.openChannel("exec");
((ChannelExec) channel1).setPty(true);
((ChannelExec) channel1).setCommand(command);
// X Forwarding
// channel.setXForwarding(true);
//channel.setInputStream(System.in);
channel1.setInputStream(null);
//channel.setOutputStream(System.out);
//FileOutputStream fos=new FileOutputStream("/tmp/stderr");
//((ChannelExec)channel).setErrStream(fos);
((ChannelExec) channel1).setErrStream(System.err);
InputStream in1 = channel1.getInputStream();
channel1.connect();
byte[] tmp1 = new byte[1024];
while (true) {
while (in1.available() > 0) {
int i = in1.read(tmp1, 0, 1024);
if (i < 0) break;
System.out.print(new String(tmp1, 0, i));
}
if (channel1.isClosed()) {
if (in1.available() > 0) continue;
System.out.println("exit-status: " + channel1.getExitStatus());
break;
}
try {
Thread.sleep(1000);
} catch (Exception ee) {
}
}
} catch (Exception e) {
}
}
By above code my other Linux command are working. But docker exec it not working. And the other option i tried
try {
String host = "root@10.121.21.224";
String passwd = "root#10";
Terminal exec = new Terminal(pageUiUtils);
exec.loginSSH(host, passwd);
String[] command = {"docker", "exec", "-it", "netvertex", "bash"};
ProcessBuilder pb = new ProcessBuilder(command);
pb.inheritIO();
Process proc = pb.start();
InputStream is = proc.getInputStream();
OutputStream os = proc.getOutputStream();
BufferedReader reader
= new BufferedReader(new InputStreamReader(is));
BufferedWriter writer
= new BufferedWriter(new OutputStreamWriter(os));
writer.write("pwd");
writer.flush();
String line = "";
while ((line = reader.readLine()) != null) {
System.out.print(line + "\n");
}
exec.execute("docker exec -it netvertex bash");
pageUiUtils.pause(1000);
exec.execute("pwd");
//proc.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
by above code also showing error that
The input device is not a TTY
-it
to the commandpwd
or what is going to be the full scale vision?!