-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.java
133 lines (107 loc) · 4.28 KB
/
main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class NetworkChecker extends Application {
Stage window;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
window = primaryStage;
window.setTitle("Wi-FI Connection Checker");
//GridPane with 10px padding around edge
GridPane grid = new GridPane();
grid.setPadding(new Insets(10, 10, 10, 10));
grid.setVgap(10);
grid.setHgap(10);
//Name Label - constrains use (child, column, row)
Label nameLabel = new Label("Find my IP Address:");
nameLabel.setId("bold-label");
GridPane.setConstraints(nameLabel, 0, 0);
//Search IP Address
Button IPlookupButton = new Button("Search for IP");
GridPane.setConstraints(IPlookupButton, 3, 0);
//Name Input
TextField nameInput = new TextField();
nameInput.setPromptText("IP Address");
GridPane.setConstraints(nameInput, 1, 0);
IPlookupButton.setOnAction(event -> {
try {
InetAddress thisIp = InetAddress.getLocalHost();
nameInput.setText("IP:" + thisIp.getHostAddress());
} catch (Exception e) {
e.printStackTrace();
}
});
//MAC Address Lookup Label
Label passLabel = new Label("MAC Address Look UP:");
GridPane.setConstraints(passLabel, 0, 1);
//MAC Address Input
TextField passInput = new TextField();
passInput.setPromptText("MAC Address");
GridPane.setConstraints(passInput, 1, 1);
//Search MAC Address
Button MacAddressButton = new Button("Search for MAC Address");
GridPane.setConstraints(MacAddressButton, 3, 1);
MacAddressButton.setOnAction(event -> {
InetAddress ip;
try {
ip = InetAddress.getLocalHost();
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
byte[] mac = network.getHardwareAddress();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
passInput.setText(sb.toString());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
}
}
);
//Wi-Fi Connection Button
Button WifiButton = new Button("Search my Wi-Fi");
GridPane.setConstraints(WifiButton, 1, 3);
TextField WifiInfo = new TextField();
GridPane.setConstraints(WifiInfo, 1, 4);
WifiButton.setOnAction(event -> {
try {
Process process = Runtime.getRuntime().exec("arp -a");
process.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
int i = 0;
while (reader.ready()) {
i++;
String ip = reader.readLine();
if (i >= 4) {
ip = ip.substring(2, 56) + "\n";
}
WifiInfo.setText(ip);
WifiInfo.setPrefWidth(WifiInfo.getText().length() * 7);
}
} catch (IOException | InterruptedException ioe) {
ioe.printStackTrace();
}
});
//Add everything to grid
grid.getChildren().addAll(nameLabel, IPlookupButton, nameInput, passLabel, passInput, MacAddressButton, WifiButton, WifiInfo);
Scene scene = new Scene(grid, 600, 300);
window.setScene(scene);
window.show();
}
}