0

I want to run netstat -an | grep 12345 and retrieve the output to find out if a port is empty and ready to use. I have tried

TRY1:

System.out.println("RUNNING ==> "+command);
try
{
    Process process = Runtime.getRuntime().exec("netstat -an | grep 4324");
    InputStream in = process.getInputStream();
    File tmp = File.createTempFile("allConnections","txt");
    byte[] buf = new byte[256];
    OutputStream outputConnectionsToFile = new FileOutputStream(tmp);
    int numbytes = 0;

    while ((numbytes = in.read(buf, 0, 256)) != -1)
    {
        outputConnectionsToFile.write(buf, 0, numbytes);
    }

    System.out.println("File is present at "+tmp.getAbsolutePath());
}
catch (Exception e)
{
    e.printStackTrace(System.err);
}

i see 12k results, like i was doing netstat -an without grep

TRY2:

public static ArrayList<String> exec_command_dont_wait(String command) throws IOException, InterruptedException
{
    //String[] arrayExplodedArguments = command.split(" ");
    ArrayList<String> returnHolder = new ArrayList<String>();

    List<String> listCommands = new ArrayList<String>();
    String[] arrayExplodedCommands = command.split(" ");

    for(String element : arrayExplodedCommands)
    {
        listCommands.add(element);

        System.out.println(element+"\n");
    }

    System.exit(0);

    ProcessBuilder ps = new ProcessBuilder(listCommands);

    ps.redirectErrorStream(true);
    Process p = ps.start();

    BufferedReader buffer = new BufferedReader(new InputStreamReader(p.getInputStream()));

    //p.waitFor();

    String line = new String();
    while((line = buffer.readLine()) != null)
    {
        returnHolder.add(line);
        //System.out.println(line);
    }

    return returnHolder;
}

i see 12k results, like i was doing netstat -an without grep , never to mention that i have to comment //p.waitFor(); and i don't know why.

  1. How do i a simple netstat command with grep and retrieve the results?
  2. Is there a simple command/function like in PHP and retrieve the results? Like 'exec()', really love that function.
  3. What is the differance between running with p.waitFor() and without it. I mean i understand that JAVA waits for the process to finsish, but netstat seems to never finish, i waited 2 minutes. A simple curl finishes quicly.

Example

netstat -an | grep 4352
tcp        0      0 192.99.3.11:43529       118.32.42.29:22        ESTABLISHED
tcp        0      0 192.99.3.11:43522       15.139.118.57:22       ESTABLISHED
tcp        0      0 192.99.3.11:43521       116.322.199.10:22       ESTABLISHED

Thank you.

Damian
  • 761
  • 1
  • 9
  • 26
  • 1
    possible duplicate of http://stackoverflow.com/questions/5928225/how-to-make-pipes-work-with-runtime-exec ? – Max Fichtelmann Mar 10 '14 at 22:12
  • No, is not duplicate, because if you look at `exec_command_dont_wait` you see that i'm `exploding` string by space and then pass them to `ProcessBuilder ps = new ProcessBuilder(listCommands);` witch indeed is not `Process p = Runtime.getRuntime().exec(cmd);` but works the same ... – Damian Mar 10 '14 at 22:25
  • the basic problem is the same though. Pipe is a shell feature and you need to execute via `sh` to be able to pipe. – Max Fichtelmann Mar 10 '14 at 22:32
  • 2
    I would say it's a duplicate: fixing your code like `String[] cmd = { "/bin/sh", "-c", "netstat -an | grep 4352" }; try { Process process = Runtime.getRuntime().exec(cmd);` – Peter Svensson Mar 10 '14 at 22:33
  • Oh thank you. Works like thunder. http://pastebin.com/nyTm6JDx Now explain me, why `/bin/sh -C` i mean what is matter with `netstat -an | grep 1234` or `/bin/netstat -an | /bin/grep 12345` ?? Why they will not work? – Damian Mar 10 '14 at 22:51
  • 1
    @Damian `exec` runs an application = it interprets the string you give to it as the name of the application. You have to execute a shell first and give it your command as an argument. The shell will then interpret the pipe, execute the individual commands, etc. – Jakub Kotowski Mar 10 '14 at 22:54
  • ok, but what is a `pipe` and can you point me to a tutorial explaining this `pipe/passing individual concept` ? thanks – Damian Mar 11 '14 at 19:20

0 Answers0