如何使用java程序运行命令提示符命令?
这是我第一次在这里发帖,所以我不确定该说什么/问什么。 无论如何,我正在尝试制作一个简单的java程序,从java程序运行命令提示符命令,主要用于ping flood(ping flooding我自己) 这是我目前的代码如何使用java程序运行命令提示符命令?,java,command,command-prompt,ping,prompt,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
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编程相当陌生
编辑:这里是一个快速的“信息”的解释,我试图完成
试着这样做:
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
命令输出。getOutput方法有问题。看起来您打算收集每一行输出。但事实上,由于您正在将行
分配给输出
,因此只会返回流结束前的最后一行
要解决此问题,请更改
output = line;
到
或者更准确地说:
output += line + LINE_SEPARATOR;
您之前声明后者为:
final String LINE_SEPARATOR = System.getProperty("line.separator");
这并不能直接解释为什么会得到
null
,但这可能是因为您正在运行的命令正在将输出写入“error”流,而不是“output”流
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。我无法向您展示输出,因为我缺乏声誉
我不知道为什么第一行是空的。不管怎样,它是有效的
希望这对你有帮助。好好编码。试着打印进程输入流的原始输出。仍然为空,尽管我可能没有按照你的要求去做。System.out.println(in.readLine());这就是我尝试过的,现在它返回“null-1”,这并不能帮助他们了解他们做错了什么。请注意,他们的问题不是“我怎么做?”而是“为什么这不起作用?”因此,这并不能真正回答问题。^没错,谢谢。我只是注意到我忘了放“ping”“在命令中。。现在每次我按下开始按钮,包int都会上升1,这不是我想要的。输出仍然为null。仍然返回null。如果我只使用System.out.println(line),它可以工作,但不是我想要的方式。我希望它做的是打印响应,例如,当我键入“ping 127.0.0.1-t-n1”时,它将回答“reply from 127.0.0.1:bytes=32次“ping pyynt”:tuntematon是“nt”127.0.0.1:2
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;
}