Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex Python程序与正则表达式_Regex_Python 2.7 - Fatal编程技术网

Regex Python程序与正则表达式

Regex Python程序与正则表达式,regex,python-2.7,Regex,Python 2.7,您能帮我编写一些正则表达式和Python代码吗?我最近收到了很多帮助,帮助我编写Python脚本来查看一些防火墙日志。我快到了,但仍然需要一些帮助 以下是防火墙的一些日志输出: Nov 11 00:00:09 firewall %ASA-3-710003: TCP access denied by ACL from 1.1.1.1/50624 to internet:2.2.2.2/80 Nov 6 12:42:23 firewall %ASA-4-106023: Deny tcp src i

您能帮我编写一些正则表达式和Python代码吗?我最近收到了很多帮助,帮助我编写Python脚本来查看一些防火墙日志。我快到了,但仍然需要一些帮助

以下是防火墙的一些日志输出:

Nov 11 00:00:09 firewall %ASA-3-710003: TCP access denied by ACL from 1.1.1.1/50624 to internet:2.2.2.2/80
Nov  6 12:42:23 firewall %ASA-4-106023: Deny tcp src inside:3.3.3.3/42059 dst internet:4.4.4.4/389 by access-group "access_out" [0x0, 0x0]
日志可以同时具有IPv4和IPv6地址。我需要提取协议(tcp | udp | icmp)、源| ip、目标| ip、目标|端口和每个唯一行的计数器。但是从上面两行可以看出,这两行中的日志输出是不同的,例如,在一行中,协议用大写字母编写,另一行用小写字母编写,IP地址之间的文本也不同。我能够从其中一行获得正确的输出,但不能同时从两行获得正确的输出

prot   source                    destination               port   hitcnt
------ ------------------        ------------------        ------ ------------------
tcp    1111:2222:0:abcd::12      2222:3333:0:efab::101     389    180
tcp    2222:3333:0:efab::50      1111:2222:0:abcd::12      389    29
ump    1111:4444:0:1111::2       1111:2222:0:abcd::12      123    4
tcp    1.1.1.1                   3.3.3.3                   23     4
imp    2.2.2.2                   4.4.4.4                          4
python脚本中的代码如下所示(感谢holroy)

提前感谢。

测试多种模式 要测试多个复杂模式,您需要在数据文件中的一个且唯一的循环中添加这些测试。也就是说,您需要以下代码:

    for line in data :

        # If line has 'Deny ' in it, then check it some more
        if 'Deny ' in line:
            seen.update(DENY_PATTERN.findall(line)) 

        # If line has 'ASA-3' in it, then check for the other pattern
        if 'ASA-3' in line:
            seen.update(DENIED_PATTERN.findall(line))
def print_counter(counter):
    """Pretty print the result of the counter."""

    print(LINE_FORMAT.format('prot', 'source', 'destination',  'port', 'hitcnt'))
    print(LINE_FORMAT.format(*tuple(('------------------',) * 5)))
    for re_match, count in counter.most_common():

        re_match_length = len(re_match)

        # Pick elements based on match from DENY_PATTERN        
        if re_match_length == 8:
            protocol, src, src_port, dst, dst_port, icmp_spec, icmp_type, icmp_code =  re_match
            if icmp_code or icmp_type:
                dst_port = '{}, {}'.format(icmp_code, icmp_type)

        # Pick elements based on match from DENIED_PATTERN
        if re_match_length == 4:
            protocol, src, dst, dst_port = re_match


        print(LINE_FORMAT.format(protocol.lower(), src, dst, dst_port, count))
通过这种方式,您只需读取一次文件,并且仅当针对
Deny
ASA-3
的基本测试已经发生时,才执行较重的模式匹配

Regexp注释 在regexp中,您有一些不需要的额外组,您可以针对
[a-Z].*.
进行测试,这是一个大写字符,后跟任何内容的非贪婪匹配。除非这是您想要的特定内容,否则最好使用
[A-Z]*?
(或
[A-Za-Z]*?
)来匹配字符序列。我冒昧地保存了一些额外的版本,以以下内容结束(请参阅):

为了稍微美化输出,我添加了一个
protocol.lower()
,以便它们总是以小写形式显示。这也可以在相应的
if
语句中完成,同时在输出行之前进行其他必要的调整。

测试多个模式 要测试多个复杂模式,您需要在数据文件中的一个且唯一的循环中添加这些测试。也就是说,您需要以下代码:

    for line in data :

        # If line has 'Deny ' in it, then check it some more
        if 'Deny ' in line:
            seen.update(DENY_PATTERN.findall(line)) 

        # If line has 'ASA-3' in it, then check for the other pattern
        if 'ASA-3' in line:
            seen.update(DENIED_PATTERN.findall(line))
def print_counter(counter):
    """Pretty print the result of the counter."""

    print(LINE_FORMAT.format('prot', 'source', 'destination',  'port', 'hitcnt'))
    print(LINE_FORMAT.format(*tuple(('------------------',) * 5)))
    for re_match, count in counter.most_common():

        re_match_length = len(re_match)

        # Pick elements based on match from DENY_PATTERN        
        if re_match_length == 8:
            protocol, src, src_port, dst, dst_port, icmp_spec, icmp_type, icmp_code =  re_match
            if icmp_code or icmp_type:
                dst_port = '{}, {}'.format(icmp_code, icmp_type)

        # Pick elements based on match from DENIED_PATTERN
        if re_match_length == 4:
            protocol, src, dst, dst_port = re_match


        print(LINE_FORMAT.format(protocol.lower(), src, dst, dst_port, count))
通过这种方式,您只需读取一次文件,并且仅当针对
Deny
ASA-3
的基本测试已经发生时,才执行较重的模式匹配

Regexp注释 在regexp中,您有一些不需要的额外组,您可以针对
[a-Z].*.
进行测试,这是一个大写字符,后跟任何内容的非贪婪匹配。除非这是您想要的特定内容,否则最好使用
[A-Z]*?
(或
[A-Za-Z]*?
)来匹配字符序列。我冒昧地保存了一些额外的版本,以以下内容结束(请参阅):


为了稍微美化输出,我添加了一个
protocol.lower()
,以便它们总是以小写形式显示。这也可以在相应的
if
语句中完成,并在输出行之前进行其他必要的调整。

Hi reach@joni,这个问题更适合这里,如果您愿意,我们可以在一个我创建的…Hi reach@joni中讨论与这个问题相关的一些方面,这个问题更适合这里,如果你想的话,我们可以在我创建的。。。
def print_counter(counter):
    """Pretty print the result of the counter."""

    print(LINE_FORMAT.format('prot', 'source', 'destination',  'port', 'hitcnt'))
    print(LINE_FORMAT.format(*tuple(('------------------',) * 5)))
    for re_match, count in counter.most_common():

        re_match_length = len(re_match)

        # Pick elements based on match from DENY_PATTERN        
        if re_match_length == 8:
            protocol, src, src_port, dst, dst_port, icmp_spec, icmp_type, icmp_code =  re_match
            if icmp_code or icmp_type:
                dst_port = '{}, {}'.format(icmp_code, icmp_type)

        # Pick elements based on match from DENIED_PATTERN
        if re_match_length == 4:
            protocol, src, dst, dst_port = re_match


        print(LINE_FORMAT.format(protocol.lower(), src, dst, dst_port, count))