How to use java program to run command line commands?

This is my first post here, so I'm not quite sure what to say / ask. In any case, I am trying to create a simple java program that runs command line commands from a java program mainly used for ping flood (ping flood).

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 am new to java, so maybe I will not do what I want. I want it to be this, enter the IP address in the text box and divide it by ":", and after that the number of packets, for example

 127.0.0.1:100 

Although now, when I try to use this number of ip and packets, it returns "null-0" (from the exec method), and I'm not even sure that he did anything related to ping.

What I'm trying to accomplish is the same as I said, flood ping itself, and then output everything that I get as an answer, although I have no idea if this code really does something even with this, I'm in I mainly 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; } 

Can someone explain to me why my code is not working, how I want it to work? Please do not judge, as I said, I am completely new to Java programming.

EDIT: Here is a short “informative” explanation of what I'm trying to accomplish.

  • I am dialing an ip address and how many packets I want to send. In this explanation, I use localhost ip and 5 packets. About to send 5 packets to localhost ip
  • I'm starting an attack. In this part, I want the program to run the cmd command line command

    ping 127.0.0.1 -t -n 5

    127.0.0.1 is the ip that I put in the text box in my program, and 5 is the number of packets that I put in the text box.

  • I started the attack, so this is what should happen on the command line: 5 packets sent to locahost

    The language is Finnish, but still the same.

    This is the main explanation of what I'm trying to accomplish, I hope someone understood and can help / say why my code is not working, or it works, but does not print the correct lines in the eclipse console.

+8
java command command-prompt prompt ping
source share
3 answers

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) {} 

You will then need to read the out variable to parse the output of the ping command.

+4
source share

There is a problem with your getOutput method. It looks like you are going to collect every line of output. But actually, since you assign line to output , you only return the last line to the end of the stream.

To fix this, change

  output = line; 

to

  output += line + "\n"; 

Or rather

  output += line + LINE_SEPARATOR; 

where you previously declared the latter as:

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

This does not directly explain why you get null , but it could be because the command you run writes the output to the "error" stream, and not to the "output" stream.

+4
source share
 bAttack.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String input = ipTextField.getText(); String[] value = input.split(":"); int amountOfPackets = Integer.parseInt(value[1]); try { p=Runtime.getRuntime().exec("ping -n "+amountOfPackets+" "+value[0]); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } running = true; } 

Just a small modification to your code. get the conclusion:

 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 =output+ line+"\n"; packets++; } return output; } catch (IOException e) { System.err.println(e.getStackTrace()); } return null; } 

Here's the output of JTextArea, which I accepted to display the output of the PING process. I can’t show you the result because I don’t have enough reputation.

I do not know why the first line is null. Anyway, it works.

Hope this helps you. Good coding time.

0
source share

All Articles