如何使用C#确定是否为远程计算机启用了防火墙 [英] How to determine whether a firewall is enabled of a Remote Machine using c#

查看:101
本文介绍了如何使用C#确定是否为远程计算机启用了防火墙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何确定远程计算机的防火墙状态?

我使用了以下代码,
1)

How to determine firewall status of a remote machine?

I used following codes,
1)

try
{
   TcpClient tcp = new TcpClient();
    tcp.Connect("RemoteIP", Convert.ToInt16(135));
    Console.WriteLine("online");
}
catch (Exception ex)
{
    Console.WriteLine("offline");
    Console.WriteLine(ex.Message);
}




2)




2)

Type NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", REMOTEIP, false);
INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType);
Firewallenabled = mgr.LocalPolicy.CurrentProfile.FirewallEnabled;
return Firewallenabled;

---------------------------------------------- ------------------------------


*如果防火墙是开/关,第一个将始终连接到端口135
*第二个将正确确定状态,但是,它要求用户位于远程计算机的管理员组中,并且我的机器"是64位的,而远程机器"是32位的,这会引发COM错误





任何其他解决方法或建议均受欢迎,
请回复
预先谢谢你

问候
纳特卡

[edit]添加了代码块[/edit]

----------------------------------------------------------------------------


* The first one will always connects to the port 135 if though Firewall is ON/OFF
* The Second one will determine the status correctly but,it requires user to be in the administrator group of remote machines and My Machine is of 64 bit and remote machine is of 32 bit which throws COM ERROR





Any other work around or Suggestions are welcomed,
Plz do reply
thank you in advance

Regards
Natekar

[edit]code blocks added[/edit]

推荐答案

除非您使用第二个代码段,否则没有可靠的方法确定远程防火墙的状态.出于安全原因就是这样.

是否可以连接到端口与该计算机上防火墙的状态无关.如果无法连接到端口,则说明防火墙正在运行,或者正在侦听该端口的服务器未侦听或未运行.无法分辨.
Unless you use the second code snippet, there is no reliable way for you to determine the status of a remote firewall. It''s that way for security reasons.

If you can or cannot connect to a port has no bearing on the status of the firewall on that machine. If you cannot connect to the port, either the firewall is running or the server listening on that port is not listening or not running. There is no way to tell.


这篇关于如何使用C#确定是否为远程计算机启用了防火墙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆