简体   繁体   English

检测 Windows 防火墙是否阻止了我的程序

[英]Detect if windows firewall is blocking my program

I have an application that communicates with a NetApp device through their api.我有一个应用程序通过他们的 api 与 NetApp 设备进行通信。 With the windows firewall on, the api commands will fail.在 Windows 防火墙打开的情况下,api 命令将失败。 With the firewall off, the api commands work.关闭防火墙后,api 命令可以工作。 I don't receive any message like "Windows Firewall is blocking this program".我没有收到“Windows 防火墙正在阻止此程序”之类的任何消息。

Looking through the documentation I believe that I found the TCP ports that need to be open for the api commands to work.查看文档,我相信我找到了需要打开才能使 api 命令工作的 TCP 端口。 How can I programatically detect if the ports are blocked so I can display a message to the user on the potential problem?如何以编程方式检测端口是否被阻止,以便向用户显示有关潜在问题的消息?

防火墙管理器通过 COM 暴露自己并实现IsPortAllowed

You can do it like this I think: give it a try: Change 1433 for the port you want to check.我认为您可以这样做:试一试:将 1433 更改为要检查的端口。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;

namespace CheckPortStatus
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                TcpClient tcp = new TcpClient();
                tcp.Connect("localhost", Convert.ToInt16(1433));
                Console.WriteLine("online");
            }
            catch (Exception ex)
            {
                Console.WriteLine("offline");
                Console.WriteLine(ex.Message);
            }
        }
    }
}

Also, to see which ports are available in your machine run:此外,要查看您的机器中可用的端口,请运行:

C:>netstat -an |find /i "listening" C:>netstat -an |find /i "listening"

TCP 0.0.0.0:25 0.0.0.0:0 LISTENING TCP 0.0.0.0:25 0.0.0.0:0 监听

TCP 0.0.0.0:80 0.0.0.0:0 LISTENING TCP 0.0.0.0:80 0.0.0.0:0 监听

TCP 0.0.0.0:135 0.0.0.0:0 LISTENING TCP 0.0.0.0:135 0.0.0.0:0 监听

To detect if the ports are blocked - on Win7 you can view the Window Firewall logs by opening Windows Firewall - click Advanced Settings on the left-side and then open the Monitoring branch.要检测端口是否被阻止 - 在 Win7 上,您可以通过打开 Windows 防火墙查看 Window 防火墙日志 - 单击左侧的高级设置,然后打开监控分支。

Note on the Monitoring tab in the Logging Settings section there is an option to log to file which on my Win7 PC is %systemroot%\\system32\\LogFiles\\Firewall\\pfirewall.log - you could just parse this file.注意在日志设置部分的监控选项卡上有一个选项可以记录到文件,在我的 Win7 PC 上是%systemroot%\\system32\\LogFiles\\Firewall\\pfirewall.log - 你可以解析这个文件。 I have researched in the past and there are utilities out there to do this for you, however, at the end of the day it's just a standard format log file.我过去研究过,有一些实用程序可以为您执行此操作,但是,归根结底,它只是一个标准格式的日志文件。

I doubt that the firewall will mention that it's blocking the application, otherwise intruder can have a information on what's preventing him to access the system :-).我怀疑防火墙是否会提到它正在阻止应用程序,否则入侵者可以获得有关阻止他访问系统的信息:-)。

Usually, firewalls logs attempts to connect from and to the computer, successful or not, you can check it.通常,防火墙会记录与计算机之间的连接尝试,无论成功与否,您都可以检查它。

Update*更新*

you may try Acknowledgement in the network.您可以尝试在网络中确认 If you received none for certain amount of time, then you can safely say that there's a problem in the connection.如果您在一定时间内没有收到任何信息,那么您可以肯定地说连接存在问题。

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

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