我正在尝试创建一个程序来捕获mac地址连接。为此,我使用/var/log/arpalert.log文件并创建一个dict,结果是JSON。我是Python的初学者。你知道吗
现在,我有一个arpalert日志文件.txt. 我获取这个文件并在另一个文件中创建一个字典。我将结果转换为JSON格式。 但问题是我字典的“格式”。在Json格式中,一个连接的所有信息都在一个键中,另一个连接的所有信息都在键的值中。你知道吗
但我想要的是一个值为地址的密钥“mac”,一个值为地址的密钥“ip”,等等。。。你知道吗
这是我目前的代码:
from json import dumps, dump
from itertools import izip
def get_log(source, destination):
log = open(source, 'r')
fil_dest = open(destination, 'w')
txt = log.readline()
while txt:
fil_dest.write(txt)
txt = log.readline()
log.close()
fil_dest.close()
def to_json(source, destination):
fil_dest = open(destination, 'w')
with open(source, 'r') as lines:
txt = lines.readlines()
wanted_lines = txt[0:]
i = iter(wanted_lines)
dict_log = dict(izip(i, i))
dump(dict_log, fil_dest, sort_keys=True, indent=1)
lines.close()
fil_dest.close()
get_log('/var/log/arpalert.log', 'arpalert_strings.txt')
to_json('arpalert_strings.txt', 'json_conversion.txt')
以及JSON中的结果(“key”:“value”)
"Oct 8 14:45:52 arpalert: seq=2, mac=a2:e5:68:3c:16:f1, ip=192.168.1.19, type=new, dev=p3p1, vendor=\"(null)\"\n": "Oct 8 14:45:52 arpalert: seq=3, mac=04:e8:ca:a8:6f:b8, ip=192.168.1.19, type=new, dev=p3p1, vendor=\"(null)\"\n",
"Oct 9 09:41:46 arpalert: Auto selected device: bluetooth0\n": "Oct 9 09:41:46 arpalert: [./capture.c 181] ioctl[19] : No such device\n",
有人能帮我吗?我试过用正则表达式,但我迷路了,我失去了理智!你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐