4

My goal is to print all the internet connections on my computer. When I type netstat on CMD, I get the internet connections list. I wanted to do the same in Java, automatically.

My code:

Runtime runtime = Runtime.getRuntime();
process = runtime.exec(pathToCmd);
byte[] command1array = command1.getBytes(); // writing netstat in an array of bytes
OutputStream out = process.getOutputStream();
out.write(command1array);
out.flush();
out.close();
readCmd();  //read and print cmd

But with this code I get C:\eclipse\workspace\Tracker>Mais? instead of the list of connections. Obviously I'm working with Eclipse, in Windows 7. What am I doing wrong? I've looked in similar topics but I couldn't find what's wrong. Thank you for the answers.

EDIT:

public static void readCmd() throws IOException {
    is = process.getInputStream();
    isr = new InputStreamReader(is);
    br = new BufferedReader(isr);
    String line;
    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }
}
Abra
  • 19,142
  • 7
  • 29
  • 41
Miigueel
  • 143
  • 2
  • 13

4 Answers4

0

Try this : I was able to create a file in my default temporary directory with all the connections

final String cmd = "netstat -ano";

        try {

            Process process = Runtime.getRuntime().exec(cmd);

            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);
        }
sreeprasad
  • 3,242
  • 3
  • 27
  • 33
0

File NETSTAT.EXE should be on the PATH (environment variable), hence your "command" of just netstat should be enough. If NETSTAT.EXE is not on the PATH, you can either edit the PATH or provide the full path to the file in your Java code. On my Windows 10 machine, the path to the file is...

C:\Windows\System32\NETSTAT.EXE

You should also use class ProcessBuilder rather than class Runtime. Since JDK 7, class ProcessBuilder has method inheritIO. Hence if all you want is to print the output of netstat, the following is the simplest code to achieve that. Note that the below code works because file NETSTAT.EXE is on the PATH.

import java.io.IOException;

public class Solution {
    public static void main(String[] args) {
        ProcessBuilder pb = new ProcessBuilder("netstat");
        pb.inheritIO();
        try {
            pb.start();
        }
        catch (IOException x) {
            x.printStackTrace();
        }
    }
}

Here are the first few lines of output that are displayed when I run the above code.


Active Connections

  Proto  Local Address          Foreign Address        State
  TCP    127.0.0.1:5354         BDL-DLRC88V:49669      ESTABLISHED
Abra
  • 19,142
  • 7
  • 29
  • 41
-1

You can also use an instance of java.util.Scanner to read the output of the command.

public static void main(String[] args) throws Exception {
    String[] cmdarray = { "netstat", "-o" };
    Process process = Runtime.getRuntime().exec(cmdarray);
    Scanner sc = new Scanner(process.getInputStream(), "IBM850");
    sc.useDelimiter("\\A");
    System.out.println(sc.next());
    sc.close();
}
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
-2
final String cmd = "netstat -ano";

    try {

        Process process = Runtime.getRuntime().exec(cmd);

        InputStream in = process.getInputStream();
        InputStreamReader isr = new InputStreamReader(in);
        BufferedReader br = new BufferedReader(isr);
        String line;

        while ((line = br.readLine()) != null) {
           System.out.println(line);
        }


    } catch (Exception e) {
        e.printStackTrace(System.err);
    } finally{
        in  = null;
        isr = null;
        br = null;
    }
rollstuhlfahrer
  • 3,988
  • 9
  • 25
  • 38
  • 2
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – rollstuhlfahrer Apr 10 '18 at 21:32