How to disable Windows Firewall using python

Ohads picture Ohads · Jan 30, 2016 · Viewed 8.6k times · Source

I am trying to write automation to a little project that I'm doing in work. In the proccess I need to disable Windows Firewall (for every Windows version) using python (I prefer activepython because it already installed).

I looked for many answers but I didn't found any answer that suits my needs.

I found this site: https://mail.python.org/pipermail/python-win32/2012-July/012434.html But the problem is that when I check from the control panel the actual disabling of Firewall is not happening...

Can someone help me with this problem?

Answer

asdfasdfadsf picture asdfasdfadsf · Jan 30, 2016

The best way to do it would be using WMI:

import wmi,os

c = wmi.WMI("WinMgmts:\root\Microsoft\HomeNet")

for obj in c.HNet_ConnectionProperties():
    print obj
    print obj.IsFirewalled
    obj.IsFirewalled = False
    obj.Put_()

Of course to do this you will need to be running the program as an administrator.

Hope this helps,

Jason.