네트워크 목록 작성할때 IP 대역에서 활성화된 device 목록을 얻을 수 있다.
import java.io.BufferedReader;
import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.InetAddress; import java.net.URL; import java.util.regex.Matcher; import java.util.regex.Pattern; public class IPScanner { public static void main(String[] args) throws Exception { // Define the subnet (adjust it to your network) String subnet = "192.168.20"; // Change this to your subnet (e.g., 192.168.0) System.out.println("Starting IP Scan on subnet: " + subnet); System.out.println("---------------------------------------------------"); // Loop through all IP addresses in the subnet (1 to 254) for (int i = 1; i <= 30; i++) { String host = subnet + "." + i; // Ping the host to check if it's reachable try { if (pingHost(host)||true) { System.out.println("Host: " + host); // Get the device (hostname) String deviceName = getHostName(host); System.out.println("Device Name: " + deviceName); // Get the MAC address String macAddress = getMacAddress(host); System.out.println("MAC Address: " + macAddress); // Get the manufacturer String manufacturer = getManufacturer(macAddress); System.out.println("Manufacturer: " + manufacturer); System.out.println("---------------------------------------------------"); } }catch(Exception e) { e.printStackTrace(); } } } // Function to ping the host to check if it's reachable public static boolean pingHost(String host) { try { InetAddress inet = InetAddress.getByName(host); return inet.isReachable(1000); // Timeout in milliseconds } catch (Exception e) { return false; } } // Function to get the host/device name (hostname) using reverse DNS lookup public static String getHostName(String host) { try { InetAddress inet = InetAddress.getByName(host); return inet.getHostName(); // Returns the hostname if available } catch (Exception e) { return "Unknown Device"; } } // Function to get the MAC address by running the ARP command public static String getMacAddress(String host) { try { // Run the ARP command Process p = Runtime.getRuntime().exec("arp -a " + host); BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); String line; while ((line = reader.readLine()) != null) { if (line.contains(host)) { // Use regex to extract the MAC address String regex = "([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(line); if (matcher.find()) { return matcher.group(0); } } } } catch (Exception e) { e.printStackTrace(); } return "MAC Address not found"; } // Function to get Manufacturer using the macvendors API based on the MAC address public static String getManufacturer(String macAddress) { if (macAddress.equals("MAC Address not found")) { return "Manufacturer not found"; } try { // Call macvendors API URL url = new URL("https://api.macvendors.com/" + macAddress); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); // Read the response from the API while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // Return the manufacturer name return response.toString(); } catch (Exception e) { e.printStackTrace(); } return "Manufacturer not found"; } } |