Example #1
0
    def do_lint(self,string,lint=True):
        import signal
        import subprocess
        retval = []

        if lint is True:
            (old_ret,old_res) = self.do_status()
            if old_ret is True:
                old_lines = []
                cmdfile = "cmd:%s" % PARSER_COMMAND_IPTABLES_SAVE
                self.parser.set_source_file([cmdfile])
                conf_arr = self.parser.read_conf()
                try:
                    old_lines = conf_arr[cmdfile]['value']
                except:
                    pass
                self.parser.set_source_file([PARSER_IPTABLES_CONF])


        signal.alarm(10)
        if lint is True:
            command_args = [PARSER_COMMAND_IPTABLES_RESTORE,"--test"]
        else:
            command_args = [PARSER_COMMAND_IPTABLES_RESTORE]
        proc = subprocess.Popen(command_args,
                   bufsize=1,
                   shell=True,
                   stdin=subprocess.PIPE,
                   stdout=subprocess.PIPE,
                   stderr=subprocess.PIPE)

        #proc.stdin.write(string)
        (stdout,stderr) = proc.communicate(string)
        ret = proc.wait()
        signal.alarm(0)

        exclude_strings = [
           "Try `iptables-restore -h' or 'iptables-restore --help' for more information.",
           "iptables-restore v[0-9\.]+: iptables-restore:",
           "iptables-restore v[0-9\.]+: ",
        ]

        new_stderr = []
        for _aline in re.split("[\r\n]+",stderr):
            new_stderr.append(_aline)
        new_stderr = array_replace(new_stderr,exclude_strings,["","",""])
        stderr = "\n".join(new_stderr)
        """
        """

        retval = [ret,stdout,stderr]

        if lint is True:
            if old_ret is True and len(old_lines) != 0:
                self.do_lint("\n".join(old_lines),lint=False)
            elif old_ret is False:
                self.do_stop()

        return retval
Example #2
0
    def do_lint(self, string, lint=True):
        import signal
        import subprocess
        retval = []

        if lint is True:
            (old_ret, old_res) = self.do_status()
            if old_ret is True:
                old_lines = []
                cmdfile = "cmd:%s" % PARSER_COMMAND_IPTABLES_SAVE
                self.parser.set_source_file([cmdfile])
                conf_arr = self.parser.read_conf()
                try:
                    old_lines = conf_arr[cmdfile]['value']
                except:
                    pass
                self.parser.set_source_file([PARSER_IPTABLES_CONF])

        signal.alarm(10)
        if lint is True:
            command_args = [PARSER_COMMAND_IPTABLES_RESTORE, "--test"]
        else:
            command_args = [PARSER_COMMAND_IPTABLES_RESTORE]
        proc = subprocess.Popen(command_args,
                                bufsize=1,
                                shell=True,
                                stdin=subprocess.PIPE,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE)

        #proc.stdin.write(string)
        (stdout, stderr) = proc.communicate(string)
        ret = proc.wait()
        signal.alarm(0)

        exclude_strings = [
            "Try `iptables-restore -h' or 'iptables-restore --help' for more information.",
            "iptables-restore v[0-9\.]+: iptables-restore:",
            "iptables-restore v[0-9\.]+: ",
        ]

        new_stderr = []
        for _aline in re.split("[\r\n]+", stderr):
            new_stderr.append(_aline)
        new_stderr = array_replace(new_stderr, exclude_strings, ["", "", ""])
        stderr = "\n".join(new_stderr)
        """
        """

        retval = [ret, stdout, stderr]

        if lint is True:
            if old_ret is True and len(old_lines) != 0:
                self.do_lint("\n".join(old_lines), lint=False)
            elif old_ret is False:
                self.do_stop()

        return retval
Example #3
0
    def write_conf(self,conf_arr={},extra_args=None,dryrun=False):
        retval = True

        now = time.strftime("%c",time.localtime())
        try:
            self.dop.addconf("parser",{})

            lines = conf_arr["config"]["value"]
            lines = array_replace(lines,PARSER_IPTABLES_CONF_HEADER,"# Generated by karesansui on %s" % (now,))
            lines = array_replace(lines,PARSER_IPTABLES_CONF_FOOTER,"# Completed on %s" % (now,))
            self.dop.set("parser",[PARSER_IPTABLES_CONF],lines)
            #self.dop.preprint_r("parser")
            arr = self.dop.getconf("parser")
            self.parser.write_conf(arr,dryrun=dryrun)
            self.do_condrestart()
        except:
            pass

        return retval
Example #4
0
    def write_conf(self, conf_arr={}, extra_args=None, dryrun=False):
        retval = True

        now = time.strftime("%c", time.localtime())
        try:
            self.dop.addconf("parser", {})

            lines = conf_arr["config"]["value"]
            lines = array_replace(lines, PARSER_IPTABLES_CONF_HEADER,
                                  "# Generated by karesansui on %s" % (now, ))
            lines = array_replace(lines, PARSER_IPTABLES_CONF_FOOTER,
                                  "# Completed on %s" % (now, ))
            self.dop.set("parser", [PARSER_IPTABLES_CONF], lines)
            #self.dop.preprint_r("parser")
            arr = self.dop.getconf("parser")
            self.parser.write_conf(arr, dryrun=dryrun)
            self.do_condrestart()
        except:
            pass

        return retval
Example #5
0
def evaluate_macro(string, macros={}):
    from karesansui.lib.utils import array_replace

    lines = string.split("\n")

    pattern = []
    replace = []
    for _k, _v in macros.iteritems():
        pattern.append("%%{%s}" % _k)
        replace.append(str(_v))
    """
    ff = open("/tmp/replace-debug.log","a")
    ff.write(str(pattern)+"\n")
    ff.write(str(replace)+"\n")
    ff.close()
    """
    return "\n".join(array_replace(lines, pattern, replace, mode="g"))
Example #6
0
def evaluate_macro(string,macros={}):
    from karesansui.lib.utils import array_replace

    lines = string.split("\n")

    pattern = []
    replace = []
    for _k,_v in macros.iteritems():
        pattern.append("%%{%s}" % _k)
        replace.append(str(_v))

    """
    ff = open("/tmp/replace-debug.log","a")
    ff.write(str(pattern)+"\n")
    ff.write(str(replace)+"\n")
    ff.close()
    """
    return "\n".join(array_replace(lines,pattern,replace,mode="g"))