import subprocess import os class EpoptesServer: def __init__(self): self.allowed_ips=set() self.del_epoptes_from_iptables() self.set_drop_epoptes() #def init def del_epoptes_from_iptables(self): p=subprocess.Popen(["iptables-save"],stdout=subprocess.PIPE,stderr=subprocess.PIPE) output=p.communicate()[0].split("\n") ret=[] for item in output: if "--dport 10000" in item: ret.append("iptables " + item.replace("-A","-D")) for item in ret: #print(item) os.system(item) #def parse_iptables def register_ip(self,ip): self.allowed_ips.add(ip) self.set_epoptes_in_iptables() return True #def register_ip def set_drop_epoptes(self): cmd="iptables -A INPUT -p tcp --dport 10000 -j DROP" #print(cmd) os.system(cmd) # def set_epoptes_in_iptables(self): self.del_epoptes_from_iptables() for item in self.allowed_ips: cmd="iptables -A INPUT -p tcp --dport 10000 -s %s -j ACCEPT"%item #print(cmd) os.system(cmd) self.set_drop_epoptes() return True #def set_iptables #class EpoptesServer if __name__=="__main__": es=EpoptesServer() es.register_ip("10.2.1.189") es.parse_iptables()