java实现检测Linux网络状态(附完整源码)
时间: 2024-03-13 14:46:03 浏览: 61
以下是使用Java实现检测Linux网络状态的完整源码:
```java
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class NetworkStatus {
public static void main(String[] args) {
try {
String command = "/bin/ping -c 1 google.com";
Process process = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
int exitCode = process.waitFor();
if (exitCode == 0) {
System.out.println("Network is UP");
} else {
System.out.println("Network is DOWN");
}
} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
}
}
}
```
该程序通过执行ping命令来检测Linux网络状态。如果ping命令返回0,则说明网络正常,否则网络异常。该程序可在Linux环境下使用,需要先安装ping命令。
阅读全文