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

8
这是我第一次在这里发帖,所以我不太确定该说/问什么。 无论如何,我正在尝试制作一个简单的Java程序,从该程序运行命令提示符命令,主要用于ping洪水攻击(ping flooding myself)。
以下是我的当前代码:
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 flood,然后输出我得到的任何响应,虽然我不知道这段代码是否与此相关,我在编写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地址和要发送的数据包数量。在这个说明中,我使用本地主机IP和5个数据包。 About to send 5 packets to localhost ip 2. 我开始攻击。在这部分中,我希望程序运行cmd提示符命令 ping 127.0.0.1 -t -n 5 其中,127.0.0.1是我在程序的文本框中输入的IP,5是我在文本框中输入的数据包数量。
3. 我开始攻击,所以在命令提示符中应该发生以下事情: 5 packets sent to locahost 语言是芬兰语,但仍然是同样的东西。 这是我尝试实现的基本说明,希望有人理解并能够帮助/告诉我为什么我的代码不起作用,或者正在工作但没有在eclipse控制台中打印正确的行。

尝试打印进程输入流的原始输出。 - user1131435
仍然是 null,虽然我可能没有按照你告诉我的去做。System.out.println(in.readLine()); 这就是我尝试的,现在它返回 "null null - 1"。 - Woobie
3个回答

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命令的输出。


这并不能帮助他们了解他们做错了什么。请注意,他们的问题不是“我该怎么做?”而是“为什么这不起作用?”因此,这并没有真正回答问题。 - user1131435
没错,谢谢。我刚刚注意到我忘记在命令中加上“ping”了...现在每次我按开始按钮时数据包的数量都会增加1,这不是我想要的结果。输出仍然为空。 - Woobie

4

你的getOutput方法存在问题。看起来你想要收集所有输出的行,但实际上由于你将line赋值给output,所以只会返回流结束之前的最后一行。

为了解决这个问题,请修改代码:

    output = line;

为了

    output += line + "\n";

更准确地说,
    output += line + LINE_SEPARATOR;

在此之前,您将后者声明为:

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

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

仍然返回null。如果我只使用System.out.println(line),它有点起作用,但不是我想要的。我想要它打印响应,例如,当我键入“ping 127.0.0.1 -t -n 1”时,它将回复“来自127.0.0.1的回复:字节=32时间<1ms TTL=128”,这就是我想要每次打印的内容。在我的程序中,如果我键入“127.0.0.1:5”,那么它将每次打印响应,在这种情况下,它将是5,因为这是我发送的数据包数量。使用我的当前代码,无论我发送多少数据包,它都会打印--> - Woobie
"Ping请求:未知的主机 127.0.0.1:2。" - Woobie

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进程的输出。我无法向您展示输出,因为我缺乏声望。

我不知道为什么第一行为空。不管怎样,它可以工作。

希望这能帮到您。祝您编码愉快。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接