-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.java
95 lines (76 loc) · 3.12 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
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Select input type: ");
System.out.println("1: Network Prefix");
System.out.println("2: Subnet CIDR");
System.out.println("3: Network Interface");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline left-over
List<String> ipAddresses = new ArrayList<>();
switch (choice) {
case 1:
System.out.print("Enter Network Prefix (e.g., 172.19.13.): ");
String networkPrefix = scanner.nextLine();
System.out.print("Enter start: ");
int start=scanner.nextInt();
System.out.print("Enter end: ");
int end=scanner.nextInt();
for (int i = start; i<=end; i++) {
ipAddresses.add(networkPrefix + i);
}
break;
case 2:
System.out.print("Enter CIDR (e.g., 172.19.14.0/24): ");
String cidr = scanner.nextLine();
ipAddresses = getIPsFromCIDR(cidr);
break;
case 3:
System.out.print("Enter interface name: ");
String interfaceName = scanner.nextLine();
List<String> interfaceIPs = NetworkInterfaceInfo.getInterfaceIPs(interfaceName);
ipAddresses.addAll(interfaceIPs);
break;
default:
System.out.println("Invalid choice!");
scanner.close();
return;
}
scanner.close();
//NetworkInterfaceInfo netinfo = new NetworkInterfaceInfo();
HostnameFetcher fetcher = new HostnameFetcher();
for (String ipAddress : ipAddresses) {
String hostname = fetcher.resolveHostname(ipAddress);
if (hostname != null) {
System.out.println("IP Address: " + ipAddress + " -> Hostname: " + hostname);
}
else {
System.out.println("Unable to resolve hostname for IP: " + ipAddress);
}
}
}
private static List<String> getIPsFromCIDR(String cidr) {
String[] parts = cidr.split("/");
int ip = 0;
String[] ipParts = parts[0].split("\\.");
for (int i = 0; i < 4; i++) {
ip |= Integer.parseInt(ipParts[i]) << (24 - (8 * i));
}
int netmaskInt = Integer.parseInt(parts[1], 10);
int mask = -1 << (32 - netmaskInt);
int network = ip & mask;
int broadcast = network | (~mask);
List<String> result = new ArrayList<>();
for (int currentIP = network; currentIP <= broadcast; currentIP++) {
result.add(String.format("%d.%d.%d.%d",
(currentIP >>> 24) & 0xFF,
(currentIP >>> 16) & 0xFF,
(currentIP >>> 8) & 0xFF,
currentIP & 0xFF));
}
return result;
}
}