-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_firewall.py
More file actions
69 lines (56 loc) · 1.67 KB
/
get_firewall.py
File metadata and controls
69 lines (56 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import subprocess
def get_enabled_firewall() -> str:
"""
Returns the name of the enabled firewall on the system.
Checks for UFW, Firewalld, and iptables in that order.
Returns:
str: The name of the enabled firewall ('ufw', 'firewalld', 'iptables') or 'none' if none are enabled.
"""
# Check for UFW
try:
ufw_status = subprocess.run(['ufw', 'status'], capture_output=True, text=True)
if 'Status: active' in ufw_status.stdout:
return 'ufw'
except FileNotFoundError:
pass
# Check for Firewalld
try:
firewalld_status = subprocess.run(['firewall-cmd', '--state'], capture_output=True, text=True)
if 'running' in firewalld_status.stdout:
return 'firewalld'
except FileNotFoundError:
pass
# Check for iptables
try:
iptables_status = subprocess.run(['iptables', '-L'], capture_output=True, text=True)
if iptables_status.returncode == 0:
return 'iptables'
except FileNotFoundError:
pass
return 'none'
def get_available_firewall() -> str:
"""
Returns the name of the available firewall on the system.
Checks for UFW, Firewalld, and iptables in that order.
Returns:
str: The name of the available firewall ('ufw', 'firewalld', 'iptables') or 'none' if none are available.
"""
# Check for UFW
try:
subprocess.run(['ufw', '--version'], capture_output=True, text=True)
return 'ufw'
except FileNotFoundError:
pass
# Check for Firewalld
try:
subprocess.run(['firewall-cmd', '--version'], capture_output=True, text=True)
return 'firewalld'
except FileNotFoundError:
pass
# Check for iptables
try:
subprocess.run(['iptables', '--version'], capture_output=True, text=True)
return 'iptables'
except FileNotFoundError:
pass
return 'none'