Code:
String line;
ProcessBuilder telnetProcessBuilder = new ProcessBuilder("/bin/bash");
telnetProcessBuilder.redirectErrorStream(true);
Process telnetProcess = telnetProcessBuilder.start();
BufferedReader input = new BufferedReader(new InputStreamReader(telnetProcess.getInputStream()));
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(telnetProcess.getOutputStream()));
output.write("telnet <hostname> -l <username>\n");
output.flush();
output.write("<password>\n");
output.flush();
output.write("cd <path>\n");
output.flush();
output.write("<script-file1>\n");
output.flush();
output.write("<script-file2>\n");
output.flush();
output.write("\n");
output.flush();
while((line = input.readLine())!= null)
{
System.out.println(line);
if(line.indexOf("<some final text after <script-file2>>")>-1)
{
line = input.readLine();
System.out.println(line);
break;
}
}
output.close();
input.close();
//output.write("exit\n");
//output.flush();
//telnetProcess.destroy();
According to above code the java starts a linux process /bin/bash and then telnets into remote systems where it executes few scripts for application and then it has to leave both input, output streams and /bin/bash process control to user and exit with 0 code.
Now, after making jar file i will call it from linux command (GNOME) terminal and then process should leave control of itself & IO streams to user at end of java code (its execution), where user will enter some commands like system status and diagnostics or exit when specific characters entered which is application dependent, this is the requirement.
But, the problem is java exits after the last line and so the process, even if i do not exit from or destroy the process. Yes, it is a natural behavior, but Can somebody tell me how can i achieve this, if somebody already faced the issue? well, i can not show what is contents of script files and other things you may need since its company owned.
Thanks for answers
Ashutosh