Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python3 regex support
#3
Don't use reqex for this task.

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!
Reply


Messages In This Thread
python3 regex support - by anna - Mar-12-2019, 07:41 AM
RE: python3 regex support - by perfringo - Mar-12-2019, 08:41 AM
RE: python3 regex support - by DeaD_EyE - Mar-12-2019, 09:05 AM
RE: python3 regex support - by anna - Mar-12-2019, 10:40 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Please support regex for version number (digits and dots) from a string Tecuma 4 3,276 Aug-17-2020, 09:59 AM
Last Post: Tecuma
  python3 regular expression.. support to improve anna 0 1,543 Jul-07-2020, 09:40 AM
Last Post: anna
  python3 emulate tail -f on remote server... support required anna 0 1,690 Jul-01-2020, 06:42 AM
Last Post: anna
  Gnuradio python3 is not compatible python3 xmlrpc library How Can I Fix İt ? muratoznnnn 3 4,991 Nov-07-2019, 05:47 PM
Last Post: DeaD_EyE
  Python3 Regex Help. Stealthychu 12 5,984 Nov-12-2018, 06:46 PM
Last Post: Stealthychu

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020