Home > other >  Java enter command to a process that opens a new telnet connection
Java enter command to a process that opens a new telnet connection

Time:07-12

I would like to write a Java program to control the Android emulator to do some testing, and now I have to take snapshots of the emulator when it is created and every time it has changes, so according to google, the command is like:

telnet localhost 5555
Trying ::1...
Connected to localhost.
Escape character is '^]'.
Android Console: type 'help' for a list of commands
OK
avd snapshot save 1
OK

So basically two commands, the first is to open a telnet connection and then enter avd snapshot save x command to save the snapshot.

However using the command like this:

    public static void main(String[] args) throws IOException{
            int initScore = 1000;
            int ID = 0;
            // monitor the log, check if a new activity is reached.
            // if so, take a snapshot of the current activity and save it to the snapshot folder.
            String cmd1 = "telnet localhost "   5555;
            // the static port 5557 should change to this.getVM_consolePort();
            String cmd2 = "avd snapshot save "   ID;
            ProcessBuilder pb = new ProcessBuilder("/bin/sh", "-c", cmd1, cmd2);
            Process p = pb.start();
            // read the process output
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String s = null;
            while ((s = stdInput.readLine()) != null) {
                System.out.println(s);
            }
    }

can only run the first command (which is to open telnet connection).

So could anyone tell me how to interact with a opened connection or enter command to another process in Java?

CodePudding user response:

Here's an example using grep:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;

public class Test {
    public static void main(String[] args) throws IOException {

        ProcessBuilder pb = new ProcessBuilder("/bin/sh", "-c", "grep foo");
        Process p = pb.start();

        BufferedReader stdOut = new BufferedReader(new InputStreamReader(p.getInputStream()));
        BufferedReader stdErr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        Writer stdIn = new OutputStreamWriter(p.getOutputStream());
        stdIn.write("foo1\nbar\nfoo2\n");
        stdIn.close();
        String s = null;
        while ((s = stdOut.readLine()) != null) {
            System.out.println(s);
        }
        while ((s = stdErr.readLine()) != null) {
            System.out.println(s);
        }
    }
}

I renamed stdIn to stdOut, so they are named from the point of view of the process you're running.

I read from stderr, so that you can see any problems.

CodePudding user response:

Actually I have tried this before like the code below:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;

public class Main {
    public static void main(String[] args) throws IOException {
        // int initScore = 1000;
        int ID = 0;

        Process p = Runtime.getRuntime().exec("telnet localhost 5555");

        BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
        BufferedWriter stdIn = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));

        String line;
        while ((line = stdInput.readLine()) != null) {
            System.out.println(line);
            if (line.contains("OK")){
                break;
            }
        }

        stdIn.write("avd snapshot save "   ID);
        

        while ((line = stdInput.readLine()) != null) {
            System.out.println(line);
        }
    }
}
Trying ::1...
Connected to localhost.
Escape character is '^]'.
Android Console: type 'help' for a list of commands
OK

I think the reason is that the telnet created a new shell and the writer is writing to the old shell, therefore it is not working, so I am looking for a solution to enter the new command into the new shell.

  • Related