我是Java编码,IAM学习和学习的新手,我建议我创建一个traceroute程序,该程序将继续测试所跟踪路由上的ping响应。
第一步是从用户那里获取IP并进行traceroute,使所有ip地址都能被ping通,但是我坚持这样做,当我尝试向Jtable添加traceroute输出的解析返回时,它不会累积,只需添加我这阵子的最后一个条目即可。
请不要责怪我,我学习不好。
我正试图弄清楚如何将数据添加到列表和JTable中。
该程序从其他类获取信息,该其他类将tracert运行到ip地址,并以以下格式将Tracert结果返回给Main:
13ms; 14ms; 23ms; 192.168.2.3
13ms; 14ms; 23ms; 192.168.2.1
13ms; 14ms; 23ms; 200.122.222.22
这是我的表格字段:ping1,ping2,ping3,跳
我需要将此数据放在表中,但需要是列表的所有ip,但是我只能获取最后一个。
我做了很多测试,并评论了一下,因为没有用。
Main.java
package application;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import application.Main.Pinge;
import application.NetworkDiagnostics;
import javafx.application.Application;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import java.util.Arrays;
import java.util.List;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
// Layout
FlowPane noRaiz = new FlowPane();
// Cena
Scene minhaCena = new Scene(noRaiz, 400, 500);
// Nós
TextField ip = new TextField();
// Tables
// List lista = new ArrayList<String>();
// lista.add("1;2;3;4");
// List<Pinge> lista = Arrays.asList();
// List<Pinge> lista = Arrays.asList(
// new Pinge("OK","OK","OK","OK")
// );
TableView<Pinge> tabela = new TableView<>();
TableColumn colunahop = new TableColumn<>("Hop");
TableColumn colunaping1 = new TableColumn<>("Ping1");
TableColumn colunaping2 = new TableColumn<>("Ping2");
TableColumn colunaping3 = new TableColumn<>("Ping3");
colunahop.setCellValueFactory(new PropertyValueFactory<>("hop"));
colunaping1.setCellValueFactory(new PropertyValueFactory<>("ping1"));
colunaping2.setCellValueFactory(new PropertyValueFactory<>("ping2"));
colunaping3.setCellValueFactory(new PropertyValueFactory<>("ping3"));
tabela.getColumns().addAll(colunahop, colunaping1, colunaping2, colunaping3);
Button vai = new Button();
vai.setText("vai");
Label texto = new Label();
texto.setText("");
TextArea resultadow = new TextArea();
// Adicionar elementos na cena
noRaiz.getChildren().add(ip);
noRaiz.getChildren().add(vai);
noRaiz.getChildren().add(texto);
noRaiz.getChildren().add(tabela);
// noRaiz.getChildren().add(resultadow);
vai.addEventHandler(ActionEvent.ACTION, new EventHandler<ActionEvent>() {
@SuppressWarnings("null")
@Override
public void handle(ActionEvent envent) {
String resultado = new String();
ip.setText("registro.br");
vai.setFocusTraversable(true);
texto.setText("Rastreando: " + ip.getText());
resultadow.setText("Rastreando: " + ip.getText());
resultado = application.NetworkDiagnostics.traceRoute(ip.getText());
// tabela.getItems().add(lista);
String[] result = resultado.split("\n");
String payload = null;
// lista.add(resultado);
int x1 = 0;
// lista.add(lista.lastIndexOf(lista)+1, new Pinge("TESTE","TESTE","TESTE","TESTE"));
List<Pinge> lista = null;
while (x1 < result.length) {
System.out.println(x1 + ": " + result[x1]);
String[] result1 = result[x1].split(";");
// payload = payload + result1[3] + ":" + result1[0] + ":" + result1[1] + ":" + result1[2] + "\n";
lista = Arrays.asList(new Pinge(result1[3],result1[0],result1[1],result1[2]));
// lista.add(new Pinge(result1[3],result1[0],result1[1],result1[2]));
resultadow.appendText(result1[3] + ":" + result1[0] + ":" + result1[1] + ":" + result1[2] + "\n");
//resultadow.setText(result[x1]);
x1++;
}
//List<Pinge> lista = Arrays.asList(new Pinge("TESTE","TESTE","TESTE","TESTE"));
tabela.setItems(FXCollections.observableArrayList(lista));
}
});
primaryStage.setScene(minhaCena);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static class Pinge {
private String hop;
private String ping1;
private String ping2;
private String ping3;
public Pinge(String hop, String ping1, String ping2, String ping3) {
this.hop = hop;
this.ping1 = ping1;
this.ping2 = ping2;
this.ping3 = ping3;
}
public String getHop() {
return hop;
}
public void setHop(String hop) {
this.hop = hop;
}
public String getPing1() {
return ping1;
}
public void setPing1(String ping1) {
this.ping1 = ping1;
}
public String getPing2() {
return ping2;
}
public void setPing2(String ping2) {
this.ping2 = ping2;
}
public String getPing3() {
return ping3;
}
public void setPing3(String ping3) {
this.ping3 = ping3;
}
}
public static void main(String[] args) {
launch(args);
}
}
这是执行tracert命令的班级
NetworkDiagnostics.java
package application;
import java.io.BufferedWriter;
import application.Main;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.InetAddress;
import java.util.Arrays;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class NetworkDiagnostics {
static String convertStreamToString(java.io.InputStream is) {
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
private final String os = System.getProperty("os.name").toLowerCase();
public static String traceRoute(String string) {
String route = "";
String res = "";
int ms = 0;
File arquivo = new File("c:/pedro/tracert.log");
try {
Process traceRt;
traceRt = Runtime.getRuntime().exec("tracert " + string);
// read the output from the command
route = convertStreamToString(traceRt.getInputStream());
route = route.trim().replaceAll(" ", " ").replaceAll(" ", " ").replaceAll(" ms", "ms").replaceAll(" ", ",");
String[] split = route.trim().split("\n");
//regex ip and domain
String IPADDRESS_PATTERN = "(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)";
String DOMAIN_PATTERN = "^(www\\.|)(([a-zA-Z0-9])+\\.){1,4}[a-z]+$";
Pattern patternIP = Pattern.compile(IPADDRESS_PATTERN);
// Pattern patternDo = Pattern.compile(DOMAIN_PATTERN);
int x = 1;
while (x < split.length) {
// debug
System.out.println(split[x]);
int y = 0;
String[] details = split[x].split(",");
while (y < details.length) {
Matcher matcherIP = patternIP.matcher(details[y]);
// Matcher matcherDO = patternDo.matcher(details[y]);
if (details[y].contains("ms"))
{
res = res + details[y] + ";";
}
if (matcherIP.find()) {
res = res + matcherIP.group() + "\n";
}
y++;
}
x++;
}
convertStreamToString(traceRt.getErrorStream());
} catch (IOException e) {
}
try {
if (!arquivo.exists()) {
// cria um arquivo (vazio)
arquivo.createNewFile();
}
// caso seja um diretório, é possível listar seus arquivos e diretórios
File[] arquivos = arquivo.listFiles();
// escreve no arquivo
FileWriter fw = new FileWriter(arquivo, true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(route.replaceAll("\\s", " "));
bw.newLine();
bw.close();
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return res;
}
}