java代码实现获取防火墙状态
时间: 2023-09-24 11:11:30 浏览: 218
以下是Java代码实现获取防火墙状态的示例:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class FirewallStatus {
public static void main(String[] args) {
try {
Process process = Runtime.getRuntime().exec("netsh advfirewall show allprofiles state");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
if (line.contains("State")) {
System.out.println(line);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
该代码使用`Runtime.getRuntime().exec()`方法执行`netsh advfirewall show allprofiles state`命令获取防火墙状态,并使用`BufferedReader`读取命令执行结果。读取结果时,只输出包含`State`关键字的行,即输出防火墙状态。
阅读全文