diff --git a/changelog.txt b/changelog.txt index d328a869..f97a2a5b 100644 --- a/changelog.txt +++ b/changelog.txt @@ -3,6 +3,7 @@ jc changelog 20200227 v1.8.0 - Added blkid command parser - Added last and lastb command parser +- Added who command parser - Added /etc/passwd file parser - Added /etc/shadow file parser diff --git a/docgen.sh b/docgen.sh index d1befcf1..7e6a34da 100755 --- a/docgen.sh +++ b/docgen.sh @@ -43,5 +43,6 @@ pydocmd simple jc.parsers.systemctl_luf+ > ../docs/parsers/systemctl_luf.md pydocmd simple jc.parsers.uname+ > ../docs/parsers/uname.md pydocmd simple jc.parsers.uptime+ > ../docs/parsers/uptime.md pydocmd simple jc.parsers.w+ > ../docs/parsers/w.md +pydocmd simple jc.parsers.who+ > ../docs/parsers/who.md pydocmd simple jc.parsers.xml+ > ../docs/parsers/xml.md pydocmd simple jc.parsers.yaml+ > ../docs/parsers/yaml.md diff --git a/docs/parsers/who.md b/docs/parsers/who.md new file mode 100644 index 00000000..657d6a4e --- /dev/null +++ b/docs/parsers/who.md @@ -0,0 +1,150 @@ +# jc.parsers.who +jc - JSON CLI output utility who Parser + +Usage: + + specify --who as the first argument if the piped input is coming from who + +Compatibility: + + 'linux', 'darwin', 'cygwin', 'aix', 'freebsd' + +Examples: + + $ who -a | jc --who -p + [ + { + "event": "reboot", + "time": "Feb 7 23:31", + "pid": 1 + }, + { + "user": "joeuser", + "writeable_tty": "?", + "tty": "console", + "time": "Feb 7 23:32", + "idle": "old", + "pid": 105 + }, + { + "user": "joeuser", + "writeable_tty": "+", + "tty": "ttys000", + "time": "Feb 13 16:44", + "idle": ".", + "pid": 51217, + "comment": "term=0 exit=0" + }, + { + "user": "joeuser", + "writeable_tty": "+", + "tty": "ttys003", + "time": "Feb 28 08:59", + "idle": "01:36", + "pid": 41402 + }, + { + "user": "joeuser", + "writeable_tty": "+", + "tty": "ttys004", + "time": "Mar 1 16:35", + "idle": ".", + "pid": 15679, + "from": "192.168.1.5" + } + ] + + $ who -a | jc --who -p -r + [ + { + "event": "reboot", + "time": "Feb 7 23:31", + "pid": "1" + }, + { + "user": "joeuser", + "writeable_tty": "?", + "tty": "console", + "time": "Feb 7 23:32", + "idle": "old", + "pid": "105" + }, + { + "user": "joeuser", + "writeable_tty": "+", + "tty": "ttys000", + "time": "Feb 13 16:44", + "idle": ".", + "pid": "51217", + "comment": "term=0 exit=0" + }, + { + "user": "joeuser", + "writeable_tty": "+", + "tty": "ttys003", + "time": "Feb 28 08:59", + "idle": "01:36", + "pid": "41402" + }, + { + "user": "joeuser", + "writeable_tty": "+", + "tty": "ttys004", + "time": "Mar 1 16:35", + "idle": ".", + "pid": "15679", + "from": "192.168.1.5" + } + ] + +## info +```python +info(self, /, *args, **kwargs) +``` + +## process +```python +process(proc_data) +``` + +Final processing to conform to the schema. + +Parameters: + + proc_data: (dictionary) raw structured data to process + +Returns: + + List of dictionaries. Structured data with the following schema: + + [ + { + "user": string, + "event": string, + "writeable_tty": string, + "tty": string, + "time": string, + "idle": string, + "pid": integer, + "from": string, + "comment": string + } + ] + +## parse +```python +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: + + List of dictionaries. Raw or processed structured data. + diff --git a/jc/parsers/who.py b/jc/parsers/who.py index 38baa229..69cf5ad9 100644 --- a/jc/parsers/who.py +++ b/jc/parsers/who.py @@ -10,11 +10,91 @@ Compatibility: Examples: - $ who | jc --who -p - [] + $ who -a | jc --who -p + [ + { + "event": "reboot", + "time": "Feb 7 23:31", + "pid": 1 + }, + { + "user": "joeuser", + "writeable_tty": "?", + "tty": "console", + "time": "Feb 7 23:32", + "idle": "old", + "pid": 105 + }, + { + "user": "joeuser", + "writeable_tty": "+", + "tty": "ttys000", + "time": "Feb 13 16:44", + "idle": ".", + "pid": 51217, + "comment": "term=0 exit=0" + }, + { + "user": "joeuser", + "writeable_tty": "+", + "tty": "ttys003", + "time": "Feb 28 08:59", + "idle": "01:36", + "pid": 41402 + }, + { + "user": "joeuser", + "writeable_tty": "+", + "tty": "ttys004", + "time": "Mar 1 16:35", + "idle": ".", + "pid": 15679, + "from": "192.168.1.5" + } + ] - $ who | jc --who -p -r - [] + $ who -a | jc --who -p -r + [ + { + "event": "reboot", + "time": "Feb 7 23:31", + "pid": "1" + }, + { + "user": "joeuser", + "writeable_tty": "?", + "tty": "console", + "time": "Feb 7 23:32", + "idle": "old", + "pid": "105" + }, + { + "user": "joeuser", + "writeable_tty": "+", + "tty": "ttys000", + "time": "Feb 13 16:44", + "idle": ".", + "pid": "51217", + "comment": "term=0 exit=0" + }, + { + "user": "joeuser", + "writeable_tty": "+", + "tty": "ttys003", + "time": "Feb 28 08:59", + "idle": "01:36", + "pid": "41402" + }, + { + "user": "joeuser", + "writeable_tty": "+", + "tty": "ttys004", + "time": "Mar 1 16:35", + "idle": ".", + "pid": "15679", + "from": "192.168.1.5" + } + ] """ import re import jc.utils @@ -49,14 +129,28 @@ def process(proc_data): [ { - "who": string, - "bar": boolean, - "baz": integer + "user": string, + "event": string, + "writeable_tty": string, + "tty": string, + "time": string, + "idle": string, + "pid": integer, + "from": string, + "comment": string } ] """ + for entry in proc_data: + int_list = ['pid'] + for key in int_list: + if key in entry: + try: + key_int = int(entry[key]) + entry[key] = key_int + except (ValueError): + entry[key] = None - # rebuild output for added semantic information return proc_data