简体   繁体   English

Java代码检查防火墙状态

[英]Java code to check the status of firewall

I'm building a Java application to check to status of firewall. 我正在构建一个Java应用程序以检查防火墙的状态。 In simpler words,it should give the output whether the firewall is ON or OFF(note that when i say firewall ,i mean the built-in firewall that comes with Windows OS). 用简单的话来说,它应该给出防火墙是打开还是关闭的输出(请注意,当我说防火墙时,是指Windows OS附带的内置防火墙)。 I need this code to give the status of the local machine itself. 我需要此代码来提供本地计算机本身的状态。 Basically ,what i'm trying to do is simulate the command "netsh advfirewall show allprofiles state". 基本上,我想做的是模拟命令“ netsh advfirewall show allprofiles state”。

As far as I know there are no Java APIs for checking the built in Windows firewall status, so you will probably have to resort to executing a shell command from Java. 据我所知,尚无用于检查内置Windows防火墙状态的Java API,因此您可能必须求助于从Java执行Shell命令。 An example: 一个例子:

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"

As I do not have a Windows machine at hand I do not know what netsh advfirewall show allprofiles state returns but I imagine that a simple output.toString().contains() will work. 因为我手头没有Windows计算机,所以我不知道netsh advfirewall show allprofiles state返回的内容,但是我想一个简单的output.toString().contains()可以工作。 Do not forget to catch any exceptions! 不要忘记捕获任何异常!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM