Files
CyberPanel/plogical/firewallUtilities.py

72 lines
2.0 KiB
Python
Raw Normal View History

2017-10-24 19:16:36 +05:00
import sys
import subprocess
import shutil
import CyberCPLogFileWriter as logging
import argparse
import os
import shlex
import socket
class FirewallUtilities:
@staticmethod
def addRule(proto,port,ipAddress):
2017-10-24 19:16:36 +05:00
try:
ruleFamily = 'rule family="ipv4"'
sourceAddress = 'source address="' + ipAddress + '"'
ruleProtocol = 'port protocol="' + proto + '"'
rulePort = 'port="' + port + '"'
2017-11-05 03:02:51 +05:00
command = "sudo firewall-cmd --permanent --zone=public --add-rich-rule='" + ruleFamily + " " + sourceAddress + " " + ruleProtocol + " " + rulePort + " " + "accept'"
2017-10-24 19:16:36 +05:00
cmd = shlex.split(command)
res = subprocess.call(cmd)
2017-11-05 03:02:51 +05:00
command = 'sudo firewall-cmd --reload'
2017-10-24 19:16:36 +05:00
cmd = shlex.split(command)
res = subprocess.call(cmd)
except OSError, msg:
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [addRule]")
return 0
except ValueError, msg:
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [addRule]")
return 0
return 1
@staticmethod
def deleteRule(proto, port,ipAddress):
2017-10-24 19:16:36 +05:00
try:
ruleFamily = 'rule family="ipv4"'
sourceAddress = 'source address="' + ipAddress + '"'
ruleProtocol = 'port protocol="' + proto + '"'
rulePort = 'port="' + port + '"'
2017-11-05 03:02:51 +05:00
command = "sudo firewall-cmd --permanent --zone=public --remove-rich-rule='" + ruleFamily + " " + sourceAddress + " " + ruleProtocol + " " + rulePort + " " + "accept'"
2017-10-24 19:16:36 +05:00
cmd = shlex.split(command)
res = subprocess.call(cmd)
2017-11-05 03:02:51 +05:00
command = 'sudo firewall-cmd --reload'
2017-10-24 19:16:36 +05:00
cmd = shlex.split(command)
res = subprocess.call(cmd)
except OSError, msg:
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [deleteRule]")
return 0
except ValueError, msg:
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [deleteRule]")
return 0
return 1