java 中 呼叫ping測試網路是否通
阿新 • • 發佈:2019-02-01
public static final String[] encodes = new String[] { "UTF-8", "GBK", "GB2312", "ISO-8859-1", "ISO-8859-2" };
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
boolean flag = ping("172.0.0.1");
System.out.println(flag);
ping02("172.28.20.92");
}
//第一種方法:Jdk1.5的InetAddresss,程式碼簡單。
public static boolean ping(String ipAddress) throws Exception {
int timeOut = 3000 ; //超時應該在3鈔以上
boolean status = InetAddress.getByName(ipAddress).isReachable(timeOut); // 當返回值是true時,說明host是可用的,false則不可。
return status;
}
//第二種方法:使用java呼叫cmd命令,這種方式最簡單,可以把ping的過程顯示在本地。
public static void ping02(String ipAddress) throws Exception {
String line = null;
try {
Process pro = Runtime.getRuntime().exec("ping " + ipAddress);
BufferedReader buf = new BufferedReader(new InputStreamReader(
pro.getInputStream(),"GBK"));
while ((line = buf.readLine()) != null){
//line = new String(line.getBytes("ISO-8859-1"),"UTF-8");
//line = new String(line.getBytes("UTF-8"),"GBK");
//line = new String(line.getBytes("ISO-8859-1"),"utf-8");
//line = new String(line.getBytes("ISO-8859-1"),"GBK");
//line = new String(line.getBytes("gb2312"),"GBK");
//line = new String(line.getBytes("gb2312"),"UTF-8");
//System.out.println(transcoding(line, "gbk") );
System.out.println(line);
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
// 第三種方法:也是使用java呼叫控制檯的ping命令,這個比較可靠,還通用,使用起來方便:傳入個ip,設定ping的次數和超時,就可以根據返回值來判斷是否ping通。
public static boolean ping(String ipAddress, int pingTimes, int timeOut) {
BufferedReader in = null;
Runtime r = Runtime.getRuntime(); // 將要執行的ping命令,此命令是windows格式的命令
String pingCommand = "ping " + ipAddress + " -n " + pingTimes + " -w " + timeOut;
try { // 執行命令並獲取輸出
System.out.println(pingCommand);
Process p = r.exec(pingCommand);
if (p == null) {
return false;
}
in = new BufferedReader(new InputStreamReader(p.getInputStream())); // 逐行檢查輸出,計算類似出現=23ms TTL=62字樣的次數
int connectedCount = 0;
String line = null;
while ((line = in.readLine()) != null) {
connectedCount += getCheckResult(line);
} // 如果出現類似=23ms TTL=62這樣的字樣,出現的次數=測試次數則返回真
return connectedCount == pingTimes;
} catch (Exception ex) {
ex.printStackTrace(); // 出現異常則返回假
return false;
} finally {
try {
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private static int getCheckResult(String line) { // System.out.println("控制檯輸出的結果為:"+line);
Pattern pattern = Pattern.compile("(\\d+ms)(\\s+)(TTL=\\d+)", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
return 1;
}
return 0;
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
boolean flag = ping("172.0.0.1");
System.out.println(flag);
ping02("172.28.20.92");
}
//第一種方法:Jdk1.5的InetAddresss,程式碼簡單。
public static boolean ping(String ipAddress) throws Exception {
int timeOut = 3000 ; //超時應該在3鈔以上
boolean status = InetAddress.getByName(ipAddress).isReachable(timeOut); // 當返回值是true時,說明host是可用的,false則不可。
return status;
}
//第二種方法:使用java呼叫cmd命令,這種方式最簡單,可以把ping的過程顯示在本地。
public static void ping02(String ipAddress) throws Exception {
String line = null;
try {
Process pro = Runtime.getRuntime().exec("ping " + ipAddress);
BufferedReader buf = new BufferedReader(new InputStreamReader(
pro.getInputStream(),"GBK"));
while ((line = buf.readLine()) != null){
//line = new String(line.getBytes("ISO-8859-1"),"UTF-8");
//line = new String(line.getBytes("UTF-8"),"GBK");
//line = new String(line.getBytes("ISO-8859-1"),"utf-8");
//line = new String(line.getBytes("ISO-8859-1"),"GBK");
//line = new String(line.getBytes("gb2312"),"GBK");
//line = new String(line.getBytes("gb2312"),"UTF-8");
//System.out.println(transcoding(line, "gbk") );
System.out.println(line);
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
// 第三種方法:也是使用java呼叫控制檯的ping命令,這個比較可靠,還通用,使用起來方便:傳入個ip,設定ping的次數和超時,就可以根據返回值來判斷是否ping通。
public static boolean ping(String ipAddress, int pingTimes, int timeOut) {
BufferedReader in = null;
Runtime r = Runtime.getRuntime(); // 將要執行的ping命令,此命令是windows格式的命令
String pingCommand = "ping " + ipAddress + " -n " + pingTimes + " -w " + timeOut;
try { // 執行命令並獲取輸出
System.out.println(pingCommand);
Process p = r.exec(pingCommand);
if (p == null) {
return false;
}
in = new BufferedReader(new InputStreamReader(p.getInputStream())); // 逐行檢查輸出,計算類似出現=23ms TTL=62字樣的次數
int connectedCount = 0;
String line = null;
while ((line = in.readLine()) != null) {
connectedCount += getCheckResult(line);
} // 如果出現類似=23ms TTL=62這樣的字樣,出現的次數=測試次數則返回真
return connectedCount == pingTimes;
} catch (Exception ex) {
ex.printStackTrace(); // 出現異常則返回假
return false;
} finally {
try {
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private static int getCheckResult(String line) { // System.out.println("控制檯輸出的結果為:"+line);
Pattern pattern = Pattern.compile("(\\d+ms)(\\s+)(TTL=\\d+)", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
return 1;
}
return 0;
}