|
1 |
| -package com.baeldung.ipaddresses; |
2 |
| - |
3 |
| -import org.apache.commons.net.telnet.TelnetClient; |
4 |
| -import org.apache.commons.net.util.SubnetUtils; |
5 |
| -import org.junit.jupiter.api.Test; |
6 |
| - |
7 |
| -import java.io.IOException; |
8 |
| -import java.net.InetAddress; |
9 |
| -import java.net.UnknownHostException; |
10 |
| -import java.util.ArrayList; |
11 |
| -import java.util.Arrays; |
12 |
| -import java.util.List; |
13 |
| -import java.util.stream.IntStream; |
14 |
| - |
15 |
| -import static org.junit.jupiter.api.Assertions.assertFalse; |
16 |
| - |
17 |
| -public class SubnetScannerUnitTest { |
18 |
| - |
19 |
| - @Test |
20 |
| - public void givenSubnet_whenScanningForDevices_thenReturnConnectedIPs() throws Exception { |
21 |
| - String subnet = getSubnet(); |
22 |
| - List<String> connectedIPs = new ArrayList<>(); |
23 |
| - |
24 |
| - for (int i = 1; i <= 254; i++) { |
25 |
| - String ip = subnet + "." + i; |
26 |
| - if (InetAddress.getByName(ip).isReachable(100)) { |
27 |
| - connectedIPs.add(ip); |
28 |
| - } |
29 |
| - } |
30 |
| - |
31 |
| - assertFalse(connectedIPs.isEmpty()); |
32 |
| - } |
33 |
| - |
34 |
| - @Test |
35 |
| - public void givenSubnet_whenUsingStream_thenReturnConnectedIPs() throws UnknownHostException { |
36 |
| - String subnet = getSubnet(); |
37 |
| - |
38 |
| - List<String> connectedIPs = IntStream.rangeClosed(1, 254) |
39 |
| - .mapToObj(i -> subnet + "." + i) |
40 |
| - .filter(ip -> { |
41 |
| - try { |
42 |
| - return InetAddress.getByName(ip).isReachable(100); |
43 |
| - } catch (Exception e) { |
44 |
| - return false; |
45 |
| - } |
46 |
| - }) |
47 |
| - .toList(); |
48 |
| - |
49 |
| - assertFalse(connectedIPs.isEmpty()); |
50 |
| - } |
51 |
| - |
52 |
| - @Test |
53 |
| - public void givenSubnet_whenCheckingForOpenPorts_thenReturnDevicesWithOpenPort() throws UnknownHostException { |
54 |
| - SubnetUtils utils = new SubnetUtils(getSubnet() + ".0/24"); |
55 |
| - int port = 80; |
56 |
| - List<String> devicesWithOpenPort = Arrays.stream(utils.getInfo().getAllAddresses()) |
57 |
| - .filter(ip -> { |
58 |
| - TelnetClient telnetClient = new TelnetClient(); |
59 |
| - try { |
60 |
| - telnetClient.setConnectTimeout(100); |
61 |
| - telnetClient.connect(ip, port); |
62 |
| - return telnetClient.isConnected(); |
63 |
| - } catch (Exception e) { |
64 |
| - return false; |
65 |
| - } finally { |
66 |
| - try { |
67 |
| - if (telnetClient.isConnected()) { |
68 |
| - telnetClient.disconnect(); |
69 |
| - } |
70 |
| - } catch (IOException ex) { |
71 |
| - System.err.println(ex.getMessage()); |
72 |
| - } |
73 |
| - } |
74 |
| - }) |
75 |
| - .toList(); |
76 |
| - |
77 |
| - assertFalse(devicesWithOpenPort.isEmpty()); |
78 |
| - } |
79 |
| - |
80 |
| - private String getSubnet() throws UnknownHostException { |
81 |
| - InetAddress localHost = InetAddress.getLocalHost(); |
82 |
| - byte[] ipAddr = localHost.getAddress(); |
83 |
| - return String.format("%d.%d.%d", (ipAddr[0] & 0xFF), (ipAddr[1] & 0xFF), (ipAddr[2] & 0xFF)); |
84 |
| - } |
85 |
| -} |
| 1 | +package com.baeldung.ipaddresses; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.net.InetAddress; |
| 7 | +import java.net.UnknownHostException; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.Arrays; |
| 10 | +import java.util.List; |
| 11 | +import java.util.stream.IntStream; |
| 12 | + |
| 13 | +import org.apache.commons.net.telnet.TelnetClient; |
| 14 | +import org.apache.commons.net.util.SubnetUtils; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +public class SubnetScannerUnitTest { |
| 18 | + |
| 19 | + @Test |
| 20 | + public void givenSubnet_whenScanningForDevices_thenReturnConnectedIPs() throws Exception { |
| 21 | + String subnet = getSubnet(); |
| 22 | + List<String> connectedIPs = new ArrayList<>(); |
| 23 | + |
| 24 | + for (int i = 1; i <= 254; i++) { |
| 25 | + String ip = subnet + "." + i; |
| 26 | + if (InetAddress.getByName(ip).isReachable(100)) { |
| 27 | + connectedIPs.add(ip); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + assertFalse(connectedIPs.isEmpty()); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void givenSubnet_whenUsingStream_thenReturnConnectedIPs() throws UnknownHostException { |
| 36 | + String subnet = getSubnet(); |
| 37 | + |
| 38 | + List<String> connectedIPs = IntStream.rangeClosed(1, 254) |
| 39 | + .mapToObj(i -> subnet + "." + i) |
| 40 | + .filter(ip -> { |
| 41 | + try { |
| 42 | + return InetAddress.getByName(ip).isReachable(100); |
| 43 | + } catch (Exception e) { |
| 44 | + return false; |
| 45 | + } |
| 46 | + }) |
| 47 | + .toList(); |
| 48 | + |
| 49 | + assertFalse(connectedIPs.isEmpty()); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void givenSubnet_whenCheckingForOpenPorts_thenReturnDevicesWithOpenPort() throws UnknownHostException { |
| 54 | + SubnetUtils utils = new SubnetUtils(getSubnet() + ".0/24"); |
| 55 | + int port = 80; |
| 56 | + List<String> devicesWithOpenPort = Arrays.stream(utils.getInfo().getAllAddresses()) |
| 57 | + .filter(ip -> { |
| 58 | + TelnetClient telnetClient = new TelnetClient(); |
| 59 | + try { |
| 60 | + telnetClient.setConnectTimeout(100); |
| 61 | + telnetClient.connect(ip, port); |
| 62 | + return telnetClient.isConnected(); |
| 63 | + } catch (Exception e) { |
| 64 | + return false; |
| 65 | + } finally { |
| 66 | + try { |
| 67 | + if (telnetClient.isConnected()) { |
| 68 | + telnetClient.disconnect(); |
| 69 | + } |
| 70 | + } catch (IOException ex) { |
| 71 | + System.err.println(ex.getMessage()); |
| 72 | + } |
| 73 | + } |
| 74 | + }) |
| 75 | + .toList(); |
| 76 | + |
| 77 | + assertFalse(devicesWithOpenPort.isEmpty()); |
| 78 | + } |
| 79 | + |
| 80 | + private String getSubnet() throws UnknownHostException { |
| 81 | + InetAddress localHost = InetAddress.getLocalHost(); |
| 82 | + byte[] ipAddr = localHost.getAddress(); |
| 83 | + return String.format("%d.%d.%d", (ipAddr[0] & 0xFF), (ipAddr[1] & 0xFF), (ipAddr[2] & 0xFF)); |
| 84 | + } |
| 85 | +} |
0 commit comments