【问题标题】:How can I check windows firewall status on remote server via C#如何通过 C# 检查远程服务器上的 Windows 防火墙状态
【发布时间】:2015-10-14 05:14:38
【问题描述】:

如何通过 C# 检查远程服务器上的 Windows 防火墙状态?

我使用 WMI 调用进行了调查,但 Root/SecurityCenter2 仅保存第三方防火墙信息,我一直无法找到 Windows 防火墙信息。

似乎搜索注册表可能是下一个选择,但在我走这条路之前,我想看看其他人是否有任何其他想法/建议或代码示例?

【问题讨论】:

    标签: c# windows firewall


    【解决方案1】:

    我所做的是使用 PSexec 来执行防火墙的命令:

    Process p = new Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.FileName = path; //the path of PsExec.exe
            p.StartInfo.Arguments = @"\\" + ip + " netsh advfirewall show domain state "; //you can change domain for allprofile in order to look for all profile firewall information
            p.StartInfo.RedirectStandardOutput = true;
            p.Start();
            string output = p.StandardOutput.ReadToEnd();
            Console.WriteLine(output)
            if (output.Contains("ON"))
            {
               "Domain Firewall enabled";
            }else{
               "Domain Firewall disabled";
            }
    

    PsExec : https://technet.microsoft.com/en-us/sysinternals/bb897553.aspx 只需获取 PsExec.exe

    【讨论】:

      【解决方案2】:
      string path = Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())) + "\\Resources\\PsExec.exe";
              Process p = new Process();
              p.StartInfo.UseShellExecute = false;
              p.StartInfo.CreateNoWindow = true;
              p.StartInfo.FileName = path;
              p.StartInfo.Arguments = @"\\" + ip + " netsh advfirewall show domain state ";
              p.StartInfo.RedirectStandardOutput = true;
              p.Start();
              string output = p.StandardOutput.ReadToEnd();
              if (output.Contains("ON"))
              {
                  object[] rowResult = { "0", "Server Info", "Domain Firewall enabled, it should be disabled." };
                  resultList.Add(rowResult);
              }
              else
              {
                  object[] rowResult = { "1", "Server Info", "Domain Firewall disabled." };
                  resultList.Add(rowResult);
              }
              return resultList;
      

      【讨论】:

      • 你能解释一下你的答案吗?仅代码的答案可能会被删除,因为它们没有更广泛的流通性,因为它们无法帮助他人理解导致您获得答案的步骤,从而在将来将其应用于他们自己的问题。
      • 当然,基本上我在 C# 上使用 PsExec(执行 cmd 命令的程序),然后使用进程库创建一个进程,我可以在其中将参数传递给 PsExec,例如:“\\10.0. 0.0 netsh advfirewall show domain state" 将在您的 cmd 上显示您的域防火墙的状态,因此将其用作进程参数,它将通过 PsExec 自动执行,然后使用进程 standartoutput.readToEnd() 函数来检索输出信息。
      • 感谢您的帮助
      • ...出于兴趣,您为什么要发布这个问题的三个答案?
      猜你喜欢
      • 2015-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多