Don't use reqex for this task.
A solution without using regex and relying that the output format does not change.
A solution without using regex and relying that the output format does not change.
def parse(line): date, user, nas_ip, frame_ip, station, reply, port = line.split(',') date = datetime.datetime.strptime(date, '%d-%m-%Y_%H:%M:%S') user = user.replace('User-name=', '') nas_ip = nas_ip.replace('NAS-IP-Address=', '') frame_ip = frame_ip.replace('Framed-IP-Address=', '') station = station.replace('Calling-Station-Id=', '') reply = reply.replace('Reply-Mesasge=', '') port = port.replace('NAS-Port-Id=', '').rstrip(';') port_attr = {} for item in port.split(';'): key, value = item.split('=') port_attr[key] = int(value) return { 'date': date, 'user': user, 'nas_ip': nas_ip, 'frame_ip': frame_ip, 'station': station, 'reply': reply, 'port': port_attr, }
Output:{'date': datetime.datetime(2019, 3, 11, 18, 43, 14),
'user': '4841605613',
'nas_ip': '10.124.113.246',
'frame_ip': '',
'station': '14:a7:2b:03:5f:23',
'reply': 'Authentication Failed due to Invalid Password',
'port': {'slot': 3,
'subslot': 2,
'port': 100,
'vlanid': 2160,
'vlanid2': 1284}}
Maybe the parsing of datetime is not right. Check the format. Maybe month and day is swapped.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!