如何使用java程序运行命令提示符命令?

时间:2013-04-06 14:32:23

标签: java command command-prompt ping prompt

这是我第一次在这里发帖,所以我不确定该说什么/问什么。 无论如何,我正在尝试创建一个简单的java程序,它运行来自java程序的命令提示符命令,主要用于ping flood(ping flooding)。

这是我目前的代码

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

我是java的新手,所以可能不会做我想做的事情。 我想要它做的是我在文本字段中输入一个IP地址,然后用“:”拆分它,然后分配数据包,例如

127.0.0.1:100

虽然现在当我尝试使用该ip和数据包数量时,它返回“null - 0”(来自exec方法),我甚至不确定它是否做了与ping有关的事情。

我想要完成的是正如我已经说过的那样,ping自己,然后输出我得到的任何响应,虽然我不知道这个代码是否做了任何与此相关的事情,我主要在编写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;
}

有人可以解释一下为什么我的代码不能正常工作我希望它如何工作?请不要判断,正如我已经说过的,我对java编程很陌生。

编辑:这是对我想要完成的内容的快速“信息性”解释。

  1. 我输入一个IP地址以及我想发送多少个数据包。在这个解释中,我使用的是localhost ip和5个数据包。 About to send 5 packets to localhost ip
  2. 我开始攻击。在这一部分,我希望程序运行cmd prompt命令

    ping 127.0.0.1 -t -n 5

    127.0.0.1是我在程序中的文本字段中输入的ip,5是我在文本字段中放入的数据包数量。

  3. 我开始攻击,所以这是命令提示符中应该发生的事情: 5 packets sent to locahost

    语言是芬兰语,但仍然是一样的。

    这是我想要完成的基本解释,希望有人理解并可以帮助/说明为什么我的代码不起作用,或者正在工作但不能在eclipse控制台中打印正确的行。

3 个答案:

答案 0 :(得分:4)

尝试这样的事情:

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

然后,您必须阅读out变量以连续解析ping命令输出。

答案 1 :(得分:4)

您的getOutput方法存在问题。看起来您打算收集每一行输出。但实际上,由于您要将line分配给output,因此您只会在流结束前返回最后一行。

要解决此问题,请更改

    output = line;

    output += line + "\n";

或者更正确:

    output += line + LINE_SEPARATOR;

您之前将后者声明为:

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

这并没有直接解释你获得null的原因,但这可能是因为你正在运行的命令是将输出写入错误'流而不是输出'流。

答案 2 :(得分:0)

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;


    }

只需对代码进行少量修改即可。获得输出为:

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;
}

这里的输出是JTextArea我已经采取了显示PING过程的输出。我无法向你展示输出,因为我缺乏声誉。

我不知道为什么第一行是null。无论如何,它有效。

希望这对你有所帮助。玩得开心。

相关问题