Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use java program to run command prompt commands?

this is my first time posting here, so I'm not really sure what to say/ask. Anyways, I am trying to make a simple java program that runs command prompt commands from the java program, mainly used for ping flood (ping flooding myself).

Here is my current code

public class Core extends JFrame {

JTextField ipTextField;

int packets = 0;

boolean running = false;

public Core() {
    super("Fatique");

    Container container = getContentPane();
    JButton bAttack = new JButton("Start Attack");
    JButton bStop = new JButton("Stop Attack");
    JPanel jPanel = new JPanel();

    container.setLayout(new FlowLayout());
    ipTextField = new JTextField("IP Address", 30);
    container.add(ipTextField);

    bAttack.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            String input = ipTextField.getText();
            String[] value = input.split(":");

            int amountOfPackets = Integer.parseInt(value[1]);

            exec("cmd /c" + input + " -t -n " + amountOfPackets);
            running = true;
        }
    });

    bStop.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            stop();
        }
    });

    if(!running) {
        jPanel.add(bAttack);
    } else {
        jPanel.add(bStop);
    }

    add(jPanel);
}  

public void exec(String cmd) {
    try {
        Process p = Runtime.getRuntime().exec(cmd);
        System.out.println(getOutput(p) + " - " + getPacketsSent());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public String getOutput(Process p) {
    String output = null;

    try {
        BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = null;
        while ((line = in.readLine()) != null) {
            output = line;
            packets++;
        }

        return output;
    } catch (IOException e) {
        System.err.println(e.getStackTrace());
    }

    return null;
}

public int getPacketsSent() {
    return packets;
}

public void stop() {
    exec("cmd /c break");
    running = false;
}

public static void main(String[] args) {  
    Core c = new Core();
    c.setSize(500, 300);
    c.setVisible(true);
    c.setResizable(false);
    c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    c.setLocationRelativeTo(null);
}

I'm quite new at java, so that might not do what I want it to do. What I want it to do is I enter an ip address in the textfield, and split it with ":", and after that the amount of packets, for instance

127.0.0.1:100

Though now when I try to use that ip and packet amount, it returns "null - 0" (from exec method), and I'm not even sure if it did anything related to ping.

What I am trying to accomplish is as I already said, ping flood myself, and then output whatever I get as response, though I have no idea if this code does anything even related to that, I mostly use logic when coding java.

 public String getOutput(Process p) {
    String output = null;

    try {
        BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = null;
        while ((line = in.readLine()) != null) {
            output = line;
            packets++;
        }

        return output;
    } catch (IOException e) {
        System.err.println(e.getStackTrace());
    }

    return null;
}

Could someone explain me why my code code is not working how I want it to work? Please don't judge, as I already said, I'm quite new to java programming.

EDIT: Here is a quick "informative" explanation of what I am trying to accomplish.

  1. I type in an ip address and how many packets I want to send. In this explanation, I am using localhost ip, and 5 packets. About to send 5 packets to localhost ip
  2. I start the attack. At this part, I want the program to run cmd prompt command

    ping 127.0.0.1 -t -n 5

    127.0.0.1 being the ip that I put in the textfield in my program, and 5 is the amount of packets I put in the textfield.

  3. I started the attack, so this is what should happen in the command prompt: 5 packets sent to locahost

    The language is Finnish, but still the same thing.

    This is the basic explanation of what I am trying to accomplish, hopefully someone understood and can help/tell why my code is not working, or is working but not printing the proper lines in eclipse console.

like image 479
Woobie Avatar asked Apr 06 '13 14:04

Woobie


2 Answers

There is a problem with your getOutput method. It looks like you intend to collect every line of output. But in fact, since you are assigning line to output, you will only return the last line before the end of stream.

To fix this, change

    output = line;

to

    output += line + "\n";

Or to be more correct:

    output += line + LINE_SEPARATOR;

where you previously declared the latter as:

    final String LINE_SEPARATOR = System.getProperty("line.separator");

That doesn't directly explain why you are getting null, but that might be because the command you are running is writing output to the 'error' stream rather than the 'output' stream.

like image 51
Stephen C Avatar answered Oct 29 '22 11:10

Stephen C


Try something like this:

try {
       Runtime rt = Runtime.getRuntime();
       Process p = rt.exec("ping 192.168.16.67");
       InputStream in = p.getInputStream();
       OutputStream out = p.getOutputStream ();
       InputStream err = p.getErrorStream();
       p.destroy();
} catch(Exception exc) {}

Then, you'll have to read the out variable to parse the ping command output continuously.

like image 42
Pablo Santa Cruz Avatar answered Oct 29 '22 13:10

Pablo Santa Cruz