문제

아래는 작성하려고하는 스크립트의 일부입니다.스크립트는 내 iptables 로그를 열고 로그의 각 행에는 아래 예제의 세부 정보가 들어 있습니다.

#example of a single line
#Mar  9 14:57:51 machine kernel: [23780.638839] IPTABLES Denied UDP: IN=p21p1 OUT= MAC=ff:ff:ff:ff:ff:ff:00:00:00:00:00:00:00:00 SRC=10.100.1.4 DST=10.100.1.63 LEN=78 TOS=0x00 PREC=0x00 TTL=128 ID=10898 PROTO=UDP$

# Read file in a line at a time
for line in iptables_log.readlines():
    #find time based on 4 letters, 2 spaces, up to 2 numbers, 1 space, then standard 10:10:10 time format
    time = re.findall('(^\w{1,4}\s\s\d{1,2}\s\d\d:\d\d:\d\d)', line)
    #mac lookup
    mac = re.findall('MAC=(?:\w\w:\w\w:\w\w:\w\w\:\w\w:\w\w:\w\w:\w\w:\w\w:\w\w:\w\w:\w\w:\w\w:\w\w)', line)
    #source port
    src = re.findall('SRC=(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})', line)
    #destination port
    dst = re.findall('DST=(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})', line)
    #protocol
    proto = re.findall('PROTO=(?:\w{3,4})', line)
    #sourceport
    sourceport = re.findall('SPT=(?:\w{1,5})', line)
    #destport
    destport = re.findall('DPT=(?:\w{1,5})', line)
    print time, mac, src, dst, proto, sourceport, destport
    print '======================================================'
.

스크립트가 내가 원하는 항목 만 인쇄하기 위해 스크립트를 얻으려고하지만 스크립트로 출력하면 이쪽이 같은 것으로 보이는 것 같습니다.나는 [] ''없이 인쇄하기를 원합니다.온라인으로 보면 모든 변수 (시간, 맥, src 등)는 목록 자체입니다.나는 그들을 결합하는 방법을 모르겠다.나는 참고 문헌을 보았지만이 예를 사용하는 방법을 모르겠습니다.누군가가 도움을 줄 수 있습니까?

['Mar  9 14:57:51'] ['MAC=ff:ff:ff:ff:ff:ff:00:00:00:00:00:00:00:00'] ['SRC=10.100.1.4'] ['DST=10.100.1.63'] ['PROTO=UDP'] ['SPT=137'] ['DPT=137']
.

도움이 되었습니까?

해결책

목록 포장의 압축을 풀지 만

>>> time = [0]
>>> [time] = time
>>> time
0
.

다른 팁

RE.FINDALL LIST 를 반환합니다.

def findall(pattern, string, flags=0):
    """Return a list of all non-overlapping matches in the string.

    If one or more groups are present in the pattern, return a
    list of groups; this will be a list of tuples if the pattern
    has more than one group.

    Empty matches are included in the result."""
    return _compile(pattern, flags).findall(string)
.

대신 Re.Search를 사용합니다.

>>> import re
>>> st = '''Mar  9 14:57:51 machine kernel: [23780.638839] IPTABLES Denied UDP: IN=p21p1 OUT= MAC=ff:ff:ff:ff:ff:ff:00:00:00:00:00:00:00:00 SRC=10.100.1.4 DST=10.100.1.63 LEN=78 TOS=0x00 PREC=0x00 TTL=128 ID=10898 PROTO=UDP$'''
>>> x = re.search('(^\w{1,4}\s\s\d{1,2}\s\d\d:\d\d:\d\d)',st)
>>> x.group(0)
'Mar  9 14:57:51'
.

(source= "nofollow"> http://docs.python.org/library/re.htmlps)a>)

할 수 있습니다 :

foo = re.findall(…)[0]
.

Re.FindAll 에 대해 하나의 결과만을 기대하면

re.findall 일치 목록을 반환합니다.귀하의 경우에만 하나의 값만 목록이 표시됩니다.그렇다면 항상 @ x539 답변은 목록에 첫 번째 항목을 얻습니다.

전체 행에 대해 단일 정규식 regexp를 사용하여 다음과 같은 그룹과 같은 그룹을 사용합니다.

>>> r = re.compile(r'^(?P<date>\w{1,4}\s\d{1,2}\s\d\d\:\d\d\:\d\d) (?P<hostname>\w+) kernel: (\[[0-9]+.[0-9]+\]) IN=(?P<ifacein>[a-z0-9]*) OUT=(?P<ifaceout>[a-z0-9]*) MAC=(?P<mac>[a-f0-9:]+) SRC=(?P<src>[\w.:]+) DST=(?P<dst>[\w:.]+) LEN=(?P<len>[0-9]+) TOS=0x(?P<tos>[0-9a-f]+) PREC=0x(?P<prec>[0-9a-f]+) TTL=(?P<ttl>[0-9]+) ID=(?P<id>[0-9]+) PROTO=(?P<proto>[A-Z]+) SPT=(?P<spt>[0-9]+) DPT=(?P<dpt>[0-9]+) LEN=(?P<len2>[0-9]+)')
>>> d = r.match(line).groupdict()
>>> d['dst']
'255.255.255.255'
>>> d['proto']
'UDP'
>>> d['dpt']
'17500'
.

원하는 필드가있는 단일 문자열로 모든 문자열로 쉽게 가져올 수도 있습니다.

>>> ' '.join([d[_] for _ in ("date", "mac", "src", "dst", "proto", "spt", "dpt")])
'Mar 12 13:06:10 ff:ff:ff:ff:ff:ff:00:18:8b:da:86:37:08:00 192.168.0.221 255.255.255.255 UDP 17500 17500'
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top