Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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)
     Type NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", REMOTEIP, false);
     INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType);
     Firewallenabled = mgr.LocalPolicy.CurrentProfile.FirewallEnabled;
     return Firewallenabled;
----------------------------------------------------------------------------
 

* 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]
Posted 11-Nov-12 1:51am
Edited 11-Nov-12 2:27am
Nelek42.5K
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Dnyaneshwar@Pune 959
1 Sergey Alexandrovich Kryukov 686
2 OriginalGriff 498
3 Tadit Dash 420
4 sanket saxena 327
0 Sergey Alexandrovich Kryukov 11,800
1 OriginalGriff 7,225
2 Peter Leow 5,009
3 Abhinav S 3,893
4 Maciej Los 3,575


Advertise | Privacy | Mobile
Web02 | 2.8.140421.2 | Last Updated 11 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid