Java檢測網路地址和伺服器是否可用
最近在做一個機房巡檢的小程式,希望在程式中可以檢測機房的各裝置IP是否能ping通,以及搭建有web伺服器的裝置執行是否正常,也就是能正常訪問,在網上搜索了相關資料學習後,發現要麼只有測試ping的,要麼只有測試伺服器的,所以我將他們整合起來做成一個Util,方便使用。以下是Util原始碼:
package core.util;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
public class NetStateUtil {
static HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
return true;
}
};
public String connectingAddress(String remoteInetAddr){
boolean flag=false;
String tempUrl=remoteInetAddr.substring(0, 5);//取出地址前5位
if(tempUrl.contains("http")){//判斷傳過來的地址中是否有http
if(tempUrl.equals("https")){//判斷伺服器是否是https協議
try {
trustAllHttpsCertificates();//當協議是https時
} catch (Exception e) {
e.printStackTrace();
}
HttpsURLConnection.setDefaultHostnameVerifier(hv);//當協議是https時
}
flag=isConnServerByHttp(remoteInetAddr);
}else{//傳過來的是IP地址
flag=isReachable(remoteInetAddr);
}
if(flag){
return "正常";
}else{
return "異常";
}
}
/**
* 傳入需要連線的IP,返回是否連線成功
*
* @param remoteInetAddr
* @return
*/
public static boolean isReachable(String remoteInetAddr) {// IP地址是否可達,相當於Ping命令
boolean reachable = false;
try {
InetAddress address = InetAddress.getByName(remoteInetAddr);
reachable = address.isReachable(1500);
} catch (Exception e) {
e.printStackTrace();
}
return reachable;
}
public static boolean isConnServerByHttp(String serverUrl) {// 伺服器是否開啟
boolean connFlag = false;
URL url;
HttpURLConnection conn = null;
try {
url = new URL(serverUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(3 * 1000);
if (conn.getResponseCode() == 200) {// 如果連線成功則設定為true
connFlag = true;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
conn.disconnect();
}
return connFlag;
}
/*以下是Https適用*/
private static void trustAllHttpsCertificates() throws Exception {
javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
javax.net.ssl.TrustManager tm = new miTM();
trustAllCerts[0] = tm;
javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext
.getInstance("SSL");
sc.init(null, trustAllCerts, null);
javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc
.getSocketFactory());
}
static class miTM implements javax.net.ssl.TrustManager,
javax.net.ssl.X509TrustManager {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public boolean isServerTrusted(
java.security.cert.X509Certificate[] certs) {
return true;
}
public boolean isClientTrusted(
java.security.cert.X509Certificate[] certs) {
return true;
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType)
throws java.security.cert.CertificateException {
return;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType)
throws java.security.cert.CertificateException {
return;
}
}
/*以上是Https適用*/
}
測試的時候只需要將IP地址或web伺服器的地址傳過去就可以進行檢測,檢測返回的結果是“正常”或“異常”:
NetStateUtil netStateUtil=new NetStateUtil();
String url1="https://1.1.1.1:2017";//測試https
String url2="http://1.1.1.1:2017";//測試http
String url3="1.1.1.1";//測試普通ip
System.out.println(url1+":"+netStateUtil.connectingAddress(url1));
System.out.println(url2+":"+netStateUtil.connectingAddress(url2));
System.out.println(url3+":"+netStateUtil.connectingAddress(url3));
說明一下,上面的幾個url的字串中的ip我已經更改過了,大家拿去後更改成自己想測試的ip即可