Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use java program to run command prompt commands?
    primarykey
    data
    text
    <p>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).</p> <p>Here is my current code</p> <pre><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); } </code></pre> <p>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</p> <pre><code>127.0.0.1:100 </code></pre> <p>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.</p> <p>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.</p> <pre><code> 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; } </code></pre> <p>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.</p> <p>EDIT: Here is a quick "informative" explanation of what I am trying to accomplish.</p> <ol> <li>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. <img src="https://i.stack.imgur.com/xKXbl.png" alt="About to send 5 packets to localhost ip"></li> <li><p>I start the attack. At this part, I want the program to run cmd prompt command</p> <p>ping 127.0.0.1 -t -n 5</p> <p>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.</p></li> <li><p>I started the attack, so this is what should happen in the command prompt: <img src="https://i.stack.imgur.com/SC3wz.png" alt="5 packets sent to locahost"></p> <p>The language is Finnish, but still the same thing.</p> <p>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.</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload