1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-17 00:07:37 +02:00
Files
jc/jc/parsers/last.py

219 lines
5.5 KiB
Python
Raw Normal View History

2020-08-05 16:51:58 -07:00
"""jc - JSON CLI output utility `last` and `lastb` command output parser
2020-02-27 15:14:43 -08:00
2021-01-05 13:58:38 -08:00
Supports -w and -F options.
2021-01-05 12:29:29 -08:00
2020-08-05 13:32:59 -07:00
Usage (cli):
2020-02-27 15:14:43 -08:00
2020-08-05 16:51:58 -07:00
$ last | jc --last
or
$ jc last
2020-02-27 15:14:43 -08:00
2020-08-05 13:32:59 -07:00
Usage (module):
import jc.parsers.last
result = jc.parsers.last.parse(last_command_output)
2020-02-27 15:14:43 -08:00
Compatibility:
'linux', 'darwin', 'aix', 'freebsd'
Examples:
$ last | jc --last -p
2020-02-27 15:44:36 -08:00
[
{
"user": "kbrazil",
"tty": "ttys002",
"hostname": null,
"login": "Thu Feb 27 14:31",
"logout": "still logged in"
},
{
"user": "kbrazil",
"tty": "ttys003",
"hostname": null,
"login": "Thu Feb 27 10:38",
"logout": "10:38",
"duration": "00:00"
},
{
"user": "kbrazil",
"tty": "ttys003",
"hostname": null,
"login": "Thu Feb 27 10:18",
"logout": "10:18",
"duration": "00:00"
},
...
]
2020-02-27 15:14:43 -08:00
$ last | jc --last -p -r
2020-02-27 15:44:36 -08:00
[
{
"user": "kbrazil",
"tty": "ttys002",
"hostname": "-",
"login": "Thu Feb 27 14:31",
"logout": "still_logged_in"
},
{
"user": "kbrazil",
"tty": "ttys003",
"hostname": "-",
"login": "Thu Feb 27 10:38",
"logout": "10:38",
"duration": "00:00"
},
{
"user": "kbrazil",
"tty": "ttys003",
"hostname": "-",
"login": "Thu Feb 27 10:18",
"logout": "10:18",
"duration": "00:00"
},
...
]
2020-02-27 15:14:43 -08:00
"""
import re
import jc.utils
class info():
2021-01-05 12:29:29 -08:00
version = '1.4'
2020-02-27 15:14:43 -08:00
description = 'last and lastb command parser'
author = 'Kelly Brazil'
author_email = 'kellyjonbrazil@gmail.com'
# details = 'enter any other details here'
# compatible options: linux, darwin, cygwin, win32, aix, freebsd
compatible = ['linux', 'darwin', 'aix', 'freebsd']
magic_commands = ['last', 'lastb']
__version__ = info.version
def process(proc_data):
"""
Final processing to conform to the schema.
Parameters:
2021-01-04 18:01:16 -08:00
proc_data: (List of Dictionaries) raw structured data to process
2020-02-27 15:14:43 -08:00
Returns:
2021-01-04 18:01:16 -08:00
List of Dictionaries. Structured data with the following schema:
2020-02-27 15:14:43 -08:00
[
{
2020-02-27 15:44:36 -08:00
"user": string,
"tty": string,
"hostname": string,
"login": string,
"logout": string,
"duration": string
2020-02-27 15:14:43 -08:00
}
]
"""
2020-02-27 15:44:36 -08:00
for entry in proc_data:
2020-05-30 18:42:26 -07:00
if 'user' in entry and entry['user'] == 'boot_time':
entry['user'] = 'boot time'
2020-02-27 15:44:36 -08:00
if 'tty' in entry and entry['tty'] == '~':
entry['tty'] = None
2020-02-27 15:58:12 -08:00
if 'tty' in entry and entry['tty'] == 'system_boot':
entry['tty'] = 'system boot'
2020-02-27 15:44:36 -08:00
if 'hostname' in entry and entry['hostname'] == '-':
entry['hostname'] = None
if 'logout' in entry and entry['logout'] == 'still_logged_in':
entry['logout'] = 'still logged in'
2020-02-27 15:14:43 -08:00
2020-05-09 11:22:01 -07:00
if 'logout' in entry and entry['logout'] == 'gone_-_no_logout':
entry['logout'] = 'gone - no logout'
2020-02-27 15:14:43 -08:00
return proc_data
def parse(data, raw=False, quiet=False):
"""
Main text parsing function
Parameters:
data: (string) text data to parse
raw: (boolean) output preprocessed JSON if True
quiet: (boolean) suppress warning messages if True
Returns:
2021-01-04 18:01:16 -08:00
List of Dictionaries. Raw or processed structured data.
2020-02-27 15:14:43 -08:00
"""
if not quiet:
jc.utils.compatibility(__name__, info.compatible)
raw_output = []
# Clear any blank lines
cleandata = list(filter(None, data.splitlines()))
if jc.utils.has_data(data):
2020-02-27 15:14:43 -08:00
for entry in cleandata:
output_line = {}
2020-05-30 18:42:26 -07:00
if entry.startswith('wtmp begins ') or entry.startswith('btmp begins ') or entry.startswith('utx.log begins '):
2020-02-27 15:14:43 -08:00
continue
entry = entry.replace('system boot', 'system_boot')
2020-05-30 18:42:26 -07:00
entry = entry.replace('boot time', 'boot_time')
2020-02-27 15:14:43 -08:00
entry = entry.replace(' still logged in', '- still_logged_in')
2020-05-09 11:22:01 -07:00
entry = entry.replace(' gone - no logout', '- gone_-_no_logout')
2020-02-27 15:14:43 -08:00
linedata = entry.split()
if re.match(r'[MTWFS][ouerha][nedritnu] [JFMASOND][aepuco][nbrynlgptvc]', ' '.join(linedata[2:4])):
2020-02-27 15:14:43 -08:00
linedata.insert(2, '-')
2020-05-30 18:42:26 -07:00
# freebsd fix
if linedata[0] == 'boot_time':
linedata.insert(1, '-')
linedata.insert(1, '~')
2020-02-27 15:14:43 -08:00
output_line['user'] = linedata[0]
output_line['tty'] = linedata[1]
output_line['hostname'] = linedata[2]
2021-01-05 12:29:29 -08:00
# last -F support
if re.match(r'\d\d:\d\d:\d\d \d\d\d\d', ' '.join(linedata[6:8])):
output_line['login'] = ' '.join(linedata[3:8])
if len(linedata) > 9:
output_line['logout'] = ' '.join(linedata[9:14])
if len(linedata) > 14:
output_line['duration'] = linedata[14].replace('(', '').replace(')', '')
2021-01-05 13:00:38 -08:00
# normal last support
2021-01-05 12:29:29 -08:00
else:
output_line['login'] = ' '.join(linedata[3:7])
if len(linedata) > 8:
output_line['logout'] = linedata[8]
2020-02-27 15:14:43 -08:00
2021-01-05 12:29:29 -08:00
if len(linedata) > 9:
output_line['duration'] = linedata[9].replace('(', '').replace(')', '')
2020-02-27 15:14:43 -08:00
raw_output.append(output_line)
if raw:
return raw_output
else:
return process(raw_output)