0

BGP 日志文件:bgplog.log

Host local.domain.net [11.130.55.2] with interface to BGP peer 11.130.44.65: the number of BGP UPDATE messages received changed from '110376' to '110393'
Host local.domain.net [11.130.55.2] with interface to BGP peer 11.130.44.93: the number of BGP UPDATE messages received changed from '133736' to '134146'
Host local.domain.net [11.130.55.2] with interface to BGP peer 11.130.44.65: the number of BGP UPDATE messages sent changed from '108252' to '108348'
Host local.domain.net [11.130.55.2] with interface to BGP peer 11.130.44.93: the number of BGP UPDATE messages sent changed from '2094' to '2132'
Host local.domain.net [11.130.55.2] with interface to BGP peer 11.130.44.103: the number of BGP UPDATE messages sent changed from '91440' to '91462'
Host local.domain.net [11.130.55.2] with interface to BGP peer eth1-local.domain.net [11.8.44.10]: the number of BGP UPDATE messages sent changed from '1411' to '1413'
Host local.domain.net [11.130.55.2] with interface to BGP peer 10.81.244.18: the number of BGP UPDATE messages sent changed from '112347' to '112506'
Host local.domain.net [11.130.55.2] with interface to BGP peer 11.130.44.65: the number of messages received from the remote peer changed from '538672' to '538691'
Host local.domain.net [11.130.55.2] with interface to BGP peer 11.130.44.93: the number of messages received from the remote peer changed from '547397' to '547814'

客观的:

  1. 查找特定 IP,比如说:11.130.44.93
  2. 任何行与上面的 IP 匹配,拆分行并将某些值存储到每个键。
  3. 对密钥进行排序

这是我尝试过的代码:似乎我被卡住了

import re
import os


def find(line):
    findThis = ""
    found = re.match(r'.*?11.130.44.103.*', line)
    # Find is true:
    if found:
        # found a item and adds in to findThis
        findThis = found.group()
    else:
        findThis = "NONE"
    return findThis


def generateDicts(log):
    currentDict = {}
    for line in log:
        if line.startswith(find(line)):
            currentDict = {
                "host": line.split(" ")[1][:24],
                "ip": line.split(" ", 9)[2],
                "peer": line.split(" ")[8],
                "info": line.split(" ", 9)[-1]}
        else:
            # currentDict = {"info":line.split("-",6)[-1]}
            currentDict = line


with open("bgplog.txt") as f:
    print list(generateDicts(f))

我只得到最后一个值,不知何故它没有附加。最好的方法是什么?

4

2 回答 2

0

我猜你想要一个生成器,在这种情况下你需要使用yield关键字。试试这个:

def generateDicts(log):
    for line in log:
        if line.startswith(find(line)):
            yield {
                "host": line.split(" ")[1][:24],
                "ip": line.split(" ", 9)[2],
                "peer": line.split(" ")[8],
                "info": line.split(" ", 9)[-1]}
于 2018-04-06T20:27:58.427 回答
0

一种方法可能是将字典附加到列表中。如果没有必要,不要使用正则表达式......

with open("bgplog.txt") as log:
    ip_to_search = '11.130.55.2'
    result = []

    for log_line in log:
        if ip_to_search in log_line:
            currentDict = {
                "host": log_line.split(" ")[1][:24],
                "ip": log_line.split(" ", 9)[2],
                "peer": log_line.split(" ")[8],
                "info": log_line.split(" ", 9)[-1]}
            result.append(currentDict)

    for element in result:
        print(element)
于 2018-04-06T20:48:01.003 回答