0

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

Ashutosh
  • 397
  • 1
  • 7
  • 20
  • I'm not sure that I understand correctly your question. Do you want your java process to become a deamon on the linux box where you run it? If yes, you may want to have a look [here](http://stackoverflow.com/questions/1311268/tool-for-creating-a-java-daemon-service-on-linux) – MarcoS May 03 '11 at 10:05
  • no i dont want it as deamon or startup process. but as standalone process when started should be exited by user command input. The application needs some initialization to be done before actual user can work on it in remote machine, so i use telnet into remote system (which is only option, ssh is not available in that system), running some scripts to make system ready for user and then leave the control of that process or telnet to user so user will interact and do its work and when done will give exit command manually to end the process.java should only start process & init and leave to user. – Ashutosh May 03 '11 at 11:01

1 Answers1

0

I think that your java application terminates just after the last line of script-file2, in fact that's the if condition inside your while loop.

If I understand correctly, you want your java application to (1) open a telnet connection to a remote machine, (2) automatically issue some command, then (3) let the user issue other commands manually, and finally (4) close the telnet connection when the user give an exit command. I guess (1) and (2) are done and working in your code. At that point, I thing you have to build some user-interface for your application, where the user enter commands, and you pipe those command to your telnet process (just as you do for initial commands, by using output.write(userCommand); output.flush(); and you continue doing this until the command entered by the user is exit (or something else).

MarcoS
  • 13,386
  • 7
  • 42
  • 63
  • Thanks MacroS, U r right, its better to keep java alive. Its not possible to kill java and make process still alive, so i need java to take input from user and pass to process and vice versa and this goes until user types some end string. – Ashutosh May 05 '11 at 07:11