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
| package org.colin;
import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder;
import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import java.io.IOException; import java.net.InetAddress; import java.net.Socket; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.IntStream;
public class HostSearchProcessor {
static final Pattern titlePattern = Pattern.compile("<title>.*</title>");
public static boolean tryByUrl(String url, int port) { try { final Socket socket = new Socket(url, port); socket.close(); return true; } catch (Exception e) { return false; } }
public static boolean ping(String ipAddress) { int timeOut = 3000; boolean status; try { status = InetAddress.getByName(ipAddress).isReachable(timeOut); } catch (IOException e) { return false; } return status; }
public static List<String> getHostByNetworkSegment(String networkSegment, int start, int end) { return IntStream.range(start, end).mapToObj((i) -> networkSegment + "." + i).collect(Collectors.toList()); }
public static void consoleServerTitleByNetworkSegment(String networkSegment, int start, int end, int port) throws KeyManagementException, NoSuchAlgorithmException { final CloseableHttpClient httpClient = HttpClientUtil.getHttpClientBuilder().build(); final CloseableHttpClient httpsClient = HttpClientUtil.getHttpsClientBuilder().build();
RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(3000) .setConnectionRequestTimeout(1000) .setSocketTimeout(3000).build();
final List<String> hostList = getHostByNetworkSegment(networkSegment, start, end); for (String host : hostList) { boolean isReachable = ping(host); System.out.println("ping " + host + ", res = " + isReachable); if(isReachable) { try { String url = "http://" + host + ":" + port; searchServerByUrl(httpClient, requestConfig, url); } catch (Exception e) {} try { String url = "https://" + host + ":" + port; searchServerByUrl(httpsClient, requestConfig, url); } catch (Exception e) {} } } }
private static void searchServerByUrl(CloseableHttpClient httpClient, RequestConfig requestConfig, String url) throws IOException { HttpGet httpGet = HttpClientUtil.createHttpGet(url, null); httpGet.setConfig(requestConfig); final String res = HttpClientUtil.parseRespToStr(httpClient.execute(httpGet)); final Matcher matcher = titlePattern.matcher(res); if(matcher.find()) { String title = matcher.group(0); title = title.replace("<title>", ""); title = title.replace("</title>", ""); System.out.println(url + " server title is " + title); } } }
|