gpt4 book ai didi

Java代码检查防火墙的状态

转载 作者:行者123 更新时间:2023-12-01 12:02:31 25 4
gpt4 key购买 nike

我正在构建一个 Java 应用程序来检查防火墙的状态。简而言之,它应该给出防火墙是打开还是关闭的输出(请注意,当我说防火墙时,我的意思是 Windows 操作系统附带的内置防火墙)。我需要这段代码来给出本地计算机本身的状态。基本上,我想做的是模拟命令“netsh advfirewall show allprofiles state”。

最佳答案

据我所知,没有用于检查内置 Windows 防火墙状态的 Java API,因此您可能不得不求助于从 Java 执行 shell 命令。一个例子:

StringBuilder output = new StringBuilder();
Process p = Runtime.getRuntime().exec("netsh advfirewall show allprofiles state");
p.waitFor(); //Wait for the process to finish before continuing the Java program.

BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
//output.toString() will contain the result of "netsh advfirewall show all profiles state"

由于我手头没有 Windows 计算机,我不知道 netsh advfirewall show allprofiles state 返回什么,但我想象一个简单的 output.toString().contains() 会起作用。不要忘记捕获任何异常!

关于Java代码检查防火墙的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27892125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com