【问题标题】:How to use java program to run command prompt commands?如何使用java程序运行命令提示符命令?
【发布时间】:2013-03-28 22:39:02
【问题描述】:

这是我第一次在这里发帖,所以我不确定该说什么/问什么。 无论如何,我正在尝试制作一个简单的 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 个数据包。
  2. 我开始攻击。在这部分,我希望程序运行 cmd 提示命令

    ping 127.0.0.1 -t -n 5

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

  3. 我开始了攻击,所以这就是在命令提示符下应该发生的事情:

    语言是芬兰语,但还是一样的。

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

【问题讨论】:

  • 尝试打印进程输入流的原始输出。
  • 仍然为空,尽管我可能没有按照您告诉我的去做。 System.out.println(in.readLine());这就是我尝试过的,现在它返回“null null - 1”

标签: java command command-prompt ping prompt


【解决方案1】:

您的 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”时,它会回复“Reply from 127.0.0.1: bytes=32 time
  • “Ping-pyynt”:tuntematon 是„nt„ 127.0.0.1:2。”
【解决方案2】:

试试这样的:

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

【讨论】:

  • 这并不能帮助他们了解自己做错了什么。请注意,他们的问题不是“我该怎么做?”而是“为什么这不起作用?”因此,这并不能真正回答问题。
  • ^ 没错,谢谢。我只是注意到我忘记在命令中输入“ping”了。现在,每次按下开始按钮时,packets int 都会上升 1,这不是我想要的。输出仍然为空。
【解决方案3】:
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;
}

这里的输出是我用来显示PING过程输出的JTextArea。我无法向您展示输出,因为我缺乏声誉。

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

希望这对您有所帮助。祝您编码愉快。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-11
    • 2020-08-13
    • 2010-11-30
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多